Tuples are light-weight collections used to keep track of related, but different items. Tuples are immutable, meaning that once a tuple has been created, the items in it can’t change.
You might ask, why tuples when Python already has lists? Tuples are different in a few ways. While lists are generally used to store collections of similar items together, tuples, by contrast, can be used considered to contain a snapshot of data. They can’t be continually changed, added or removed from like you could with a list.
|use||Used for storing a snapshot of related items when we don’t plan on modifying, adding, or removing data.|
|search speed||Searching for an item in a large tuple is slow. Each item must be checked.|
|common methods||Can’t add or remove from tuples.|
|order preserved?||Yes. Items can be accessed by index.|
A good use of a
tuple might be for storing the information for a row in a spreadsheet. That data is information only. We don’t necessarily care about updating or manipulating that data. We just want a read-only snapshot.
Tuples are an interesting and powerful datatype, and one that’s one of the more unique aspects of Python. Most other programming languages have ways of representing lists and dictionaries, but only a small subset contain tuples. Use them to your advantage.
One important thing to note about tuples, is there’s a quirk to their creation. Let’s check the type of an empty
tuple created with
>>> a = () >>> type(a) <class 'tuple'>
That looks like we’d expect it to. What about if we tried to create a one-item
tuple using the same syntax?
>>> b = (1) >>> type(b) <class 'int'>
It didn’t work!
type((1)) is an
integer. In order to create a one-item tuple, you’ll need to include a trailing comma.
>>> c = (1, ) >>> type(c) <class 'tuple'>
If you’re creating a one-item tuple, you must include a trailing comma, like this:
Let’s say we have a spreadsheet of students, and we’d like to represent each row as a tuple.
>>> student = ("Marcy", 8, "History", 3.5)
We can access items in the
tuple by index, but we **can’t change them.
>>> student = ("Marcy", 8, "History", 3.5) >>> student 'Marcy' >>> student = "Bob" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
TypeError: 'tuple' object does not support item assignment if we try to change the items in a tuple.
tuples also don’t have an
extend method available on them like lists do, because they can’t be changed.
Sounds like a lot of work for not a lot of benefit, right? Not so.
tuples are great when you depend on your data staying unchanged. Because of this guarantee, we can use
tuples in other types of containers like
It’s also a great way to quickly consolidate information.
You can also use
tuples for something called unpacking. Let’s see it in action:
>>> student = ("Marcy", 8, "History", 3.5) >>> >>> name, age, subject, grade = student >>> name 'Marcy' >>> age 8 >>> subject 'History' >>> grade 3.5
You can return tuples from functions, and use unpacking.
>>> def http_status_code(): ... return 200, "OK" ... >>> code, value = http_status_code() >>> code 200 >>> value 'OK'