Touple
A tuple is an ordered collection of items. An ordered collection keeps the items in the order you insert or initialize them. In other words, the order is preserved. This is in contrast to dictionaries or sets, where the order is not preserved (unordered collections).
Tuples are like lists but vary in the following aspects: They are immutable, (we cannot change them) unlike lists which are mutable (we can change them). Let us learn more about tuples and their related methods. We’ll also learn to effectively use them in Python.
For more background on the different data structures in Python, check out the following articles:
Note: Prerequisites – Make sure you have basic Python knowledge before diving into this article. It also might be a good idea to check out some linear data structures. (links are given above)
Table of Contents
Tuples: Let’s Code
As we discussed, a Tuple is a collection of items that are immutable. Let’s start by creating a tuple.
Creating a Tuple
A tuple can be created in multiple ways. The simplest way of creating a tuple is by setting a variable to a pair of empty parantheses.
The code above snippet gives an output of <class: 'tuple'>
, which indicates that the tuple has been created successfully. We can also create a tuple by using the in-built tuple()
method in Python.
While initializing a tuple, we can also specify what data exists inside it.
Accessing Items in a Tuple
Tuples follow zero indexing. In zero indexing, the first element of the tuple has the index ‘0’, the second element of the tuple has the index ‘1’, and so on.
Positive Indexing
For example, let’s create a tuple, tuple1
. Tuple elements can be accessed the same way as a list element.
This tuple follows zero indexing.
Tuple Positive Indexing: Source – GeeksforGeeks
Negative Indexing
Similar to lists, we can also use negative indexing on a tuple. Therefore, ‘-1’ refers to the Nth element of a tuple, -2 refers to the (N-1)th element, and so on (where N is the length of the tuple).
Tuple Negative Indexing
Slicing
In Python, slicing is used to return a range of values. Like lists, tuples can also be sliced.
As per the examples shown above, if we slice a range of [a : b), it would return from tuple index a to tuple index (b - 1). For more tricks on Python slicing, check out this page.
Modifying Tuples
Tuples are immutable.
For example:
If we execute the code above, the Python interpreter throws the following error:
This is because a tuple is designed to be immutable. However, we can change a tuple that contains mutable objects.
For example, let us take a tuple of lists.
This works perfectly because we are modifying the list within a tuple (which is mutable). We can also create new tuples from existing ones.
Tuple Methods
Tuples have the following in-built methods that make them extremely powerful:
cmp(tuple1, tuple2)
len(tuple)
min(tuple)
max(tuple)
tuple(list)
t.count(el)
t.index(el)
cmp(tuple1, tuple2)
Note: The cmp() method existed in python2. It wasn’t included in python3. Therefore we define our own compare method.
The compare method analyses two tuples element by element.
It compares them and returns the following:
If tuple1 > tuple2: the method returns 1.
If tuple2 > tuple1: the method returns -1.
If tuple1 == tuple2: the method returns 0.
len(tuple)
The length method returns the length of the tuple.
min(tuple)
The min method returns the smallest element in the tuple.
max(tuple)
The max method returns the largest element in the tuple.
tuple(list)
The tuple method converts the list that is passed as parameter into a tuple.
t.count(el)
The count method returns the count of the element passed as parameter.
t.index(el)
The index method returns the index of the first occurence of the element in a tuple.
You can also return the index of the last occurence of the element by using this method.
It’s also possible to specify a range to search.
Applications of Tuples
Tuples are especially used as protection against modification. Since they are immutable, we can use tuples to write-protect data.
When iterating over a tuple, a considerable performance gain is observed when we compare it to lists. This is more evident when the size of the tuple is large. Using the
timeit
module in Python, we see that tuples are considerably faster to iterate when compared to lists.
Note: For more in-depth analysis of why tuples perform better, check out this StackOverflow thread.
The dictionary data structure has an immutable key. Therefore tuples can be used as a key in a dictionary.
Tuples can be used to group related data. For example, a row in a database table can be grouped together and stored in a tuple.
Last updated