Differences between a list and a tuple in Python

    list
    • list is mutable
    • not efficient in memory usage
    • operations on a list are typically slower
    • not hashable, so cannot be used as a dictionary key
    • list is flexible; there are 11 methods for a list
    • list uses square brackets and usage of square brackets is mandatory
    tuple
    • tuple is immutable
    • uses memory efficiently
    • operations on a tuple are typically faster
    • can be a dict key if all its members are hashable
    • tuple is not flexible; there are 2 methods for tuple
    • tuple uses parentheses (except for an empty tuple, usage of parentheses is optional)
    A tuple’s memory size is fixed and don’t over-allocate. Thus, it can be stored more compactly than lists which need to over-allocate to make append() operations efficient.
    Tuples are stored in a single block of memory. Lists are allocated in two blocks: a fixed one with all the Python object information and a variable-sized block for the data.
    During execution, creating a tuple is faster than creating a list. References to objects are incorporated directly in a tuple object. In contrast, list has an extra layer of indirection to an external array of pointers. This gives tuples speed advantage for indexed lookups and unpacking. So, speed of iteration and indexing is slightly faster for tuples than lists. So, operations on tuples typically execute faster compared to operations on lists for large volumes of data.
    Lists have 11 built-in methods while tuples have only 2 built-in methods

    List methods
    1) append 2) clear 3) copy 4) count
    5) extend 6) index 7) insert 8) pop
    9) remove 10) reverse 11) sort

    Tuple methods
    1) count 2) index


difference_between_a_list_and_a_tuple
gk