What's the difference between lists and tuples?


What's the difference?

What are the advantages / disadvantages of tuples / lists?

12/12/2016 12:50:58 PM

Accepted Answer

Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.

Using this distinction makes code more explicit and understandable.

One example would be pairs of page and line number to reference locations in a book, e.g.:

my_location = (42, 11)  # page number, line number

You can then use this as a key in a dictionary to store notes on locations. A list on the other hand could be used to store multiple locations. Naturally one might want to add or remove locations from the list, so it makes sense that lists are mutable. On the other hand it doesn't make sense to add or remove items from an existing location - hence tuples are immutable.

There might be situations where you want to change items within an existing location tuple, for example when iterating through the lines of a page. But tuple immutability forces you to create a new location tuple for each new value. This seems inconvenient on the face of it, but using immutable data like this is a cornerstone of value types and functional programming techniques, which can have substantial advantages.

There are some interesting articles on this issue, e.g. "Python Tuples are Not Just Constant Lists" or "Understanding tuples vs. lists in Python". The official Python documentation also mentions this

"Tuples are immutable, and usually contain an heterogeneous sequence ...".

In a statically typed language like Haskell the values in a tuple generally have different types and the length of the tuple must be fixed. In a list the values all have the same type and the length is not fixed. So the difference is very obvious.

Finally there is the namedtuple in Python, which makes sense because a tuple is already supposed to have structure. This underlines the idea that tuples are a light-weight alternative to classes and instances.

5/14/2018 7:50:27 AM

Difference between list and tuple

  1. Literal

    someTuple = (1,2)
    someList  = [1,2] 
  2. Size

    a = tuple(range(1000))
    b = list(range(1000))
    a.__sizeof__() # 8024
    b.__sizeof__() # 9088

    Due to the smaller size of a tuple operation, it becomes a bit faster, but not that much to mention about until you have a huge number of elements.

  3. Permitted operations

    b    = [1,2]   
    b[0] = 3       # [3, 2]
    a    = (1,2)
    a[0] = 3       # Error

    That also means that you can't delete an element or sort a tuple. However, you could add new element to both list and tuple with the only difference that you will change id of the tuple by adding element

    a     = (1,2)
    b     = [1,2]  
    id(a)          # 140230916716520
    id(b)          # 748527696
    a   += (3,)    # (1, 2, 3)
    b   += [3]     # [1, 2, 3]
    id(a)          # 140230916878160
    id(b)          # 748527696
  4. Usage

    As a list is mutable, it can't be used as a key in a dictionary, whereas a tuple can be used.

    a    = (1,2)
    b    = [1,2] 
    c = {a: 1}     # OK
    c = {b: 1}     # Error
  5. Hashable Tuple is hashable while list is not. Hash functions return the hash value of the object, if it has one.

    Hashability of list and tuple.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow