oter

Tuples can be used as immutable lists from "summary" of Fluent Python by Luciano Ramalho

Tuples can be used as immutable lists. Although a tuple is not as flexible as a list, it is faster, and its immutable nature can provide some advantages. A tuple is an immutable, fixed-size, and heterogeneous sequence, just like a list. One common tuple use is as a record with no field names. Since tuples are immutable, their elements are like records fields. For example, a query function can return a tuple with the query string and a tuple of parameters. Tuples support all the list methods that do not involve adding or removing items, such as slicing and indexing. The + operator can be used to concatenate tuples to form a new tuple. If a tuple holds references to objects, those references will not change, but the objects themselves can be modified if they are mutable. However, if the referenced items are immutable, then the tuple is effectively immutable too. To build up a tuple from other iterables, the tuple constructor accepts any iterable as an argument. This can be useful when converting a list to a tuple. For instance, when receiving a variable-length argument list in a function, you may want to convert it to a tuple to ensure it remains immutable. Tuples can also be used as keys in dictionaries, as long as they contain only immutable elements themselves. This is because a tuple's hash value is computed from the hash values of the items it contains. In summary, tuples can be used as immutable lists for scenarios where immutability is desired or advantageous. They provide a simple and efficient way to represent fixed collections of items, especially when those items should not be changed.
    Similar Posts
    Denormalization can improve performance
    Denormalization can improve performance
    Denormalization, as we've seen, involves deliberately violating the rules of normalization in order to achieve certain objectiv...
    Dive into namespaces and libraries
    Dive into namespaces and libraries
    When you write a program in C or C++, you will almost certainly be using libraries. Libraries contain prewritten code that perf...
    Reduce mental clutter by capturing ideas
    Reduce mental clutter by capturing ideas
    The key to reducing mental clutter is to capture all the ideas, tasks, and commitments that are swirling around in your head. W...
    Understanding simplification techniques
    Understanding simplification techniques
    Simplification techniques are essential for solving mathematical problems quickly and accurately. These techniques involve brea...
    Debugging is the process of finding and fixing errors in code
    Debugging is the process of finding and fixing errors in code
    Debugging is the process of finding and fixing errors in code. It is an essential skill for any programmer, as even the most ex...
    Properties are used to manage attribute access
    Properties are used to manage attribute access
    Properties in Python are a way to control access to attributes. They enable you to implement getter and setter methods in a mor...
    Topological sorting orders vertices in a directed acyclic graph
    Topological sorting orders vertices in a directed acyclic graph
    Topological sorting is a crucial concept in graph theory, particularly when dealing with directed acyclic graphs. In such graph...
    Bit manipulation can optimize certain operations
    Bit manipulation can optimize certain operations
    Bit manipulation can optimize certain operations. For example, consider a program that needs to determine if a given number is ...
    Control the flow of your Python program with loops and conditionals
    Control the flow of your Python program with loops and conditionals
    Loops and conditionals are essential tools for controlling the flow of your Python program. Loops allow you to repeat a block o...
    oter

    Fluent Python

    Luciano Ramalho

    Open in app
    Now you can listen to your microbooks on-the-go. Download the Oter App on your mobile device and continue making progress towards your goals, no matter where you are.