Difference between a list and a tuple in Python





  • difference between a list and a tuple


    list
    • list is mutable
    • not efficient in memory usage
    • operations on a list are typically slower
    • cannot be used as a key for a dictionary as list is mutable
    • list is flexible
    • list uses square brackets and usage of square brackets is mandatory (cannot declare a list without them)
    tuple
    • tuple is immutable
    • uses memory efficiently
    • operations on a tuple are typically faster
    • can be used as a key for a dictionary as tuple is immutable/hashable
    • tuple is not flexible
    • tuple uses parentheses (except for an empty tuple, usage of parentheses is optional)
  • What is the difference between a list and a tuple in Python

    List uses square brackets;
    Tuple uses parentheses (except for empty tuple, use of parentheses is optional)

    lst1 = [ ]       # empty list
    lst2 = [1]       # list with one element
    lst3 = [1,]      # list with one element
    lst4 = [1,2,3]   # list with multiple elements
    lst5 = [1,2,3,]  # list with multiple elements

    tup1 = ( )       # empty tuple
    tup2 = 1,        # tuple with one element
    tup3 = (1,)      # tuple with one element
    tup4 = 1,2,3     # tuple with multiple elements
    tup5 = 1,2,3,    # tuple with multiple elements
    tup6 = (1,2,3)   # tuple with multiple elements
    tup7 = (1,2,3,)  # tuple with multiple elements
  • Differences between a tuple and a list

    Lists are mutable. So, they can be modified after creation.
    Tuple are immutable. So, once a tuple is created it cannot be modified. If a tuple needs to be modified, it has to be created anew with the new data
  • Differences between a list and a tuple

    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.
  • Differences between a tuple and a list

    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.
  • What is the difference between a tuple and a list

    As lists are mutable, they cannot be used as keys in a dictionary. This is because only an immutable/hashable object can be used as a key in a dictionary. Since tuples are immutable, we can use tuples as dictionary keys, if needed.
  • List Vs Tuple

    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
  • Tuple Vs List

    Since tuples are immutable, they do not have to be copied:
    >>> a = (1, 2, 3)
    >>> b = tuple(a)
    >>> a is b
    True

    In contrast, list(some_list) requires all the data to be copied to a new list:
    >>> a = [1, 2, 3]
    >>> b = list(a)
    >>> a is b
    False
  • Difference between a list and a tuple in Python

    Though each item stored in a list or a tuple can be of any data type, lists are typically used for homogenous objects (list of strings, list of integers etc.) and tuples are typically used for heterogenous objects (database records etc.)
  • python bootcamp 2021