I am Hack Sparrow
Captain of the Internets.

Python: difference between list and tuple

What is the difference between list and tuple in Python?

To put it lightly, tuples are lists which can't be edited. Once you create a tuple, you cannot edit it, it is immutable. Lists on the other hand are mutable, you can edit them, they work like the array object in JavaScript or PHP. You can add items, delete items from a list; but you can't do that to a tuple, tuples have a fixed size.

Here is an example demonstrating the mutable nature of lists in Python.

>>> animals = ['cat', 'dog']
>>> animals
['cat', 'dog']
>>> animals.append('mat')
>>> animals
['cat', 'dog', 'mat']
>>> animals[2] = 'bat'
>>> animals
['cat', 'dog', 'bat']

An example showing the immutable nature of tuples in Python.

>>> point = (3,7)
>>> point
(3, 7)
>>> point[1]
7
>>> point[1] = 4
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment

Technically, mutability and the syntax are the only differences between lists and tuples in Python. As a consequence of the mutability difference, tuples are easier on memory and processor, meaning you can gain some performance optimization with the use of tuples in the right places.

But there is an even more significant difference between the two - a philosophical one. I call it philosophical because the python interpreter cannot enforce it.

You might have read somewhere that tuples are heterogenous and lists are homogenous, forget that phrasing because it can be confusing and misleading. The better way to look at it is: "tuples are best used as heterogenous collections and lists are best used as homogenous collections". By heterogenous, the items contained in a tuple express different concepts, while the items in a list they all express the same concept or belong to the same category. Warning: try not to think of the homogeneity in terms of data type, think of them in terms of ideas they express or categories they belong to. Because of the heterogeneity, tuples are semantic - they have an inherent meaning to them.

Examples to demonstrate the difference between lists and tuples

i. animals = ['cat', 'rat', 'dog', 'monkey']
ii. marks = [23, 46, 67, 32, 45]
iii. comments = ['lol', 'hahaha', 'l337 hax0rrzzz', 900000]
iv. points = [(3,7),(45,23),(77,32),(3,7)]
v. movies = [('Edward Scissorhands', 'Tim Burton', 1990), ('Avatar', 'James Cameroon', 2009), ('Terminator', 'James Cameroon', 1984)]

Now let's see what the above code is all about.

  1. animals is a list of animals, all of which belong to the same category.
  2. marks is a list of marks scored by some students, a pretty homogenous data.
  3. comments is a list of comments, they may look very different, but they are all comments nonetheless.
  4. points is a list of tuples. But we aren't interested in the list, examine the tuples, do you notice the consistent data? The first item of each tuple is the x position, the next item is the y position. Making sense?
  5. movies is a list of movies. The information about the movies are contained in tuples. The first item of a tuple being the name of the movie, the second item being the director, and the last one being the release year. We use tuples like a dictionary (or JSON if you want) but without the use of key:value pair. Remember the inherent meaning thing I mentioned earlier? Savvy?

Use tuples for collections with fixed sizes, the items of which represent different but relevant data. If you feel like you need to use a dictionary or JSON, ask yourself "Can I use tuple instead? Applying the semantic thing?".

Now you may ask "Well Cap'n, I can do what those tuples do with lists, so what stops me from doing that?". Nothing, and then nothing stops you from using your cars for growing flowers either.

References

  1. tuples
  2. list

One Response to “Python: difference between list and tuple”

  1. Eli says:

    This is just such a great explanation… especially on the homogeneous and heterogeneous nature of list and tuple! Thanks!

Make a Comment