Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

At the other end of the link I post some results of my other hobbies, like playing bass. It is worth checking it out ;)

Tuples in Python

This will be a short article about tuples because sometimes you have to use them — or you just simply encounter them. And for these cases it is good to be versed what they can and what they cannot do.

This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.

We already know that tuples contain a list of values which cannot be changed. This means that you cannot change one element in the tuple. If you need to modify one element, you have to re-create the whole tuple. However you can add elements to a tuple but not with the append function.

And we know that tuples are defined between braces instead of square brackets.

This reduces the functions to use with tuples a lot so we will go through them shortly.

Tuples are indexed the same as lists. This means, you can access a value by its index and you can slice tuples.

>>> t = ("eggs", "bacon", "sausage", "Spam")
>>> t
('eggs', 'bacon', 'sausage', 'Spam')
>>> t[2]
>>> t[1:3]
('bacon', 'sausage')

As you can see, if you slice a tuple you get back another tuple.

Changing values does not work but you can extend the tuple with new elements. Let’s see what you get when you try to modify elements and how to add more data.

>> t = ("eggs", "bacon", "sausage", "Spam")
>>> t
('eggs', 'bacon', 'sausage', 'Spam')
>>> t[2] = 'baked beans'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment
>>> t += ('baked beans')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate tuple (not "str") to tuple
>>> t += ('baked beans',)
>>> t
('eggs', 'bacon', 'sausage', 'Spam', 'baked beans')
>>> del t[1]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object doesn't support item deletion

Summarizing the example above: you have to add a comma (,) after one element between braces to make it a tuple. Without this comma the element stays only a string or a number. And element deletion is not supported by tuples so you cannot remove elements from tuples. If you need to remove one element, you should use slicing and re-create the tuple.

>>> t = ("eggs", "bacon", "sausage", "Spam", "baked beans")
>>> t
('eggs', 'bacon', 'sausage', 'Spam', 'baked beans')
>>> t = t[:3]+t[-1:]
>>> t
('eggs', 'bacon', 'sausage', 'baked beans')

With the example above I removed ‘Spam’ from the tuple. As you can see, you have to convert the last element to a tuple too to enable addition. If you use only t[-1] you will get a TypeError. Go ahead and try it out.

Share the knowledge!

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below

%d bloggers like this: