All Articles

Python Tuples vs Lists

Tuples and Lists are commonly-used data structures used in Python. A lot of libraries and projects use them in different ways. They are both heterogeneous collections of items - i.e it can hold a combination of several types of data such as strings, integers etc.

They are both very similar, some of the properties they have in common include:

  • They can hold any data type (strings, integers, dictionaries etc can be stored in a tuple or a list)
  • Items in a tuple or list can be accessed via it’s index
  • They are ordered.

Seeing how similar they are, what then is the difference between a tuple and a list?

Tuples

Tuples are denoted with parentheses. Example:

names = ('bolaji',)
items = ('apple', 'oranges', 'pineapple')
items = (1,2,[12],('bill',))

Notice when we are initialising a tuple with just one item, we append a trailing comma to it. The parenthesis don’t automatically make them tuples. You have to add a comma after the string to indicate to python that it should be a tuple, however this isn’t compulsory when you have more than one item in the tuple.

You can also create tuples using the tuple constructor which takes a collection as it’s argument and converts to a tuple.

names = tuple(['bolaji'])

Lists

Lists are denoted with square brackets. It is somewhat similar to what you’ll call an array in other programming languages like Javascript.

names = ['bolaji']
items = ['apple', 'oranges', 'pineapple']

You can also create a list with the list constructor which takes a collection as it’s argument and converts to a list.

names = list(('bolaji',))
items = list(('apple', 'oranges', 'pineapple'))

The main difference between a list and a tuple is that a list is mutable while a tuple is immutable. Once you define a list, you can append, remove, shift and replace items in that list but that can’t be done with a tuple. A tuple can be regarded as a strict data structure because once defined, it can’t be changed.

Mutability refers to the liability or tendency for something to change.

Some folks get confused by what mutability means in this context as python doesn’t have the const keyword to define a variable that can’t be changed. I had this same thought when I was learning about tuples, coming from a JavaScript background. The concept of mutability here has to do with the items in the collection and not the variable itself.

Take a look at the screenshot below to understand what happens when you try to mutate a tuple.

immutability in tuples

From the screenshot above, you can see that overwriting the item at index 2 in the list is fine but when we try to do the same with a tuple we get an error.

Tuples are used whenever we want to ensure immutability in a collection so no user action can overwrite the content at the index. However, we can reassign something else to the variable store_tuple because the variable is only a container that holds a reference to the tuple we created.

Another thing to note is that though each tuple element in its self is immutable, values of mutable element in a tuple are not.

my_tuple = (8, 'kali', {'first_name': 'bran'}, [1, 5, 9])
my_tuple[3][0] = 100

print(my_tuple) // (8, 'kali', {'first_name': 'bran'}, [100, 5, 9])

Did you find this helpful? Feel free to share a comment in the comment section below.