Today we’ll be talking about tuples.
Here’s the video version of the article:
A tuple is an immutable sequence of objects.
Table of Contents
The Features Of Tuples
Here are the basic features of tuples:
– Tuples are immutable: you can’t change, add or remove any element.
– Tuples are defined via parentheses instead of square brackets.
– Square brackets are used to access the elements of tuples.
– Tuples are faster than lists.
– Tuples are safer because you can’t accidentally change any element.
– Unlike lists, tuples can be used as keys in dictionaries.
Using Tuples
A tuple containing two elements is referred to as a 2-tuple, a tuple with 3 as a 3-tuple, and so on.
Let’s define a tuple:
>>> numbers = (2, 6, 15, 7)
Let’s access its second element:
>>> numbers[1]
6
Let’s try to modify the tuple. We get an error because tuples are immutable.
>>> numbers[2] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Although it’s impossible to modify a tuple, you can still modify its particular elements provided they are mutable. Here we have a tuple containing lists.
>>> personal_data = ("John", 26, ["Anne", "David"], ["San Francisco", 1992])
First let’s try to modify the tuple by replacing the first list by a new one. Well, it doesn’t work because tuples are immutable:
>>> personal_data[2] = ["Anne", "David", "Mary"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
On the other hand lists are mutable. This means they can be modified even when inside a tuple:
>>> personal_data[2].append("Mary")
Let’s also modify the second list:
>>> personal_data[3][1] = 1995
But if we try to modify the string, it doesn’t work, as expected, because strings are immutable:
>>> personal_data[3][0][4:] = "Diego"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Here’s the tuple after all these inner modifications:
>>> personal_data
('John', 26, ['Anne', 'David', 'Mary'], ['San Francisco', 1995])
Tuples with Just One Element and Empty Tuples
If you need a tuple with just one item, you have to add a comma after this item.
>>> a = (2,)
>>> a[0]
2
Otherwise, it won’t be treated as a tuple at all, but rather as the type of whatever element you put in the parentheses. And hence you won’t be able to access it:
>>> b = (2) # treated as int, not tuple
>>> b[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
You can use b just like any other int:
>>> b + 3
5
Here we have a 1-element tuple with a string:
>>> c = ("word",)
>>> c[0]
'word'
And here we have just a string:
>>> d = ("word") # treated as string, not tuple
>>> d[0]
'w'
You can define an empty tuple like so:
>>> a = ()
Multiple Assignment
Tuples can be used to assign multiple variables in one go:
>>> first_name, last_name, age = ("Brenda", "Hopps", 35)
>>> first_name
'Brenda'
>>> last_name
'Hopps'
>>> age
35
You don’t even need the parentheses here:
>>> first_name, last_name, age = "Brenda", "Hopps", 35
>>> first_name
'Brenda'
Comma-Separated Sequences
Actually, any undefined comma-separated sequence, i.e. one not enclosed in (), [] or “”, defaults to a tuple:
>>> 5, 3, 7
(5, 3, 7)
>>> "hello", "hi", "good day"
('hello', 'hi', 'good day')