What is the difference between list and tuple in Python?#
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 = 'bat' >>> animals ['cat', 'dog', 'bat']
An example showing the immutable nature of tuples in Python.
>>> point = (3,7) >>> point (3, 7) >>> point 7 >>> point = 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> 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. Try not to think of 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:
animals = ['cat', 'rat', 'dog', 'monkey'] marks = [23, 46, 67, 32, 45] comments = ['lol', 'hahaha', 'l337 hax0rrzzz', 900000] points = [(3,7),(45,23),(77,32),(3,7)] 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.
animalsis a list of animals, all of which belong to the same category.
marksis a list of marks scored by some students, a pretty homogenous data.
commentsis a list of comments, they may look very different, but they are all comments nonetheless.
pointsis 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
xposition, the next item is the
yposition. Making sense?
moviesis 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
JSONif 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
JSON, ask yourself "Can I use
tuple instead? Applying the semantic thing?".
Now you may woder, "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.