Python Data Structures: Tuples

In Python data structures, tuples are part of the standard library. Like arrays, tuples also hold multiple values within them separated by commas. In addition, it also allows storing values of different types together. Tuples are immutable, and usually, contain a heterogeneous sequence of elements that are accessed via unpacking or indexing.

To create a tuple, we place all elements within brackets (). Unlike arrays, we need not import any module for using tuples. Let us look at some operations on the tuples python data structure now.

# Creating a tuple 'tup' with elements of the same data type
In [19]: tup = (1, 2, 3)
In [20]: tup
Out[20]: (1, 2, 3)
# Verifying the type
In [21]: type(tup)
Out[21]: tuple
# Creating a tuple 'tupl' with elements of different data types
In [22]: tupl = (1, 'a', 2.5)
In [23]: type(tupl)
Out[23]: tuple

The tuple tupl created above can be visualized in the following manner:


A tuple python data structure can also be created without using the brackets.

# Creating a tuple without brackets
In [24]: tup = 1, 2, 3
In [25]: type(tup)
Out[25]: tuple

We can repeat a value multiple times within a tuple as follows:

In [26]: tupl = (1,) * 5 # Note trailing comma
In [27]: tupl
Out[27]: (1, 1, 1, 1, 1)

Accessing tuple elements

A slice operation performed using the square brackets [] is used to access tuple elements. We pass the index value within the square brackets to get an element of our interest. Like arrays, tuples python data structures also have an index and all elements are associated with the particular index number. Again, the index starts with ‘0’.

# Access an element at index 0
In [28]: tup[0]
Out[28]: 1
# Access an element at index 2
In [29]: tup[2]
Out[29]: 1

Python throws an error if we try to access an element that does not exist. In other words, if we use the slice operation with a non-existent index, we will get an error.

In [30]: tup[3]
Traceback (most recent call last):
 File "<ipython-input-30-c965c442ca22>", line 1, in <module>
IndexError: tuple index out of range

In the above example, we try to access an element with index 3 which does not exist. Hence, Python threw an error stating index out of range.

The built-in len() function is used to check the length of a tuple.

In [31]: len(tup)
Out[31]: 3
In [32]: len(tupl)
Out[32]: 5


In Python data structures, tuple objects are immutable. That is, once they are created, it cannot be modified. If we try to modify a tuple, Python will throw an error.

In [33]: tup[1] = 10
Traceback (most recent call last):
 File "<ipython-input-33-991819cff38c>", line 1, in <module>
 tup[1] = 10
TypeError: 'tuple' object does not support item assignment

As expected, the interpreter threw an error depicting the tuple object to be immutable.

Concatenating Tuples

Python allows us to combine two or more tuples or directly concatenate new values to an existing tuple. Let us see how to concatenate tuples python data structures now:

In [34]: t1 = (1, 2, 3)
In [35]: t2 = (4, 5)
In [36]: t1 + t2
Out[36]: (1, 2, 3, 4, 5)

Tuples can be concatenated using operators *= and +=.

In [37]: t1 = (1, 2, 3)
In [38]: t1 += 4, 5
In [39]: t1
Out[39]: (1, 2, 3, 4, 5)

Unpacking Tuples

In one of the above example, we encountered the statement tup = 1, 2, 3 which is, in turn, an example of tuple packing. That is, we pack various values together into a single variable tup. The reverse operation is also possible:

In [40]: tup
Out[40]: (1, 2, 3)
In [41]: x, y, z = tup

The above statement performs the unpacking operation. It will assign the value 1 to the variable x2 to y, and 3 to z. This operation requires that there are as many variables on the left-hand side of the equal sign as there are elements in the tuples python data structures.

Tuple method

Tuple being one of the simple objects in Python data structures, is easier to maintain. There are only two methods available for tuple objects:

  • index() : This method returns the index of the element.
In [42]: tup
Out[42]: (1, 2, 3)
In [43]: tup.index(3) # Returns the index of value '3'.
Out[43]: 2
  • count() : This method counts the number of occurrences of a value.
In [44]: tup = (1, 1, 1, 1, 1)
In [45]: tup.count(1)
Out[45]: 5

Some of the reasons why tuples python data structures are useful are given below:

  • They are faster than lists.
  • They protect the data as they are immutable.
  • They can be used as keys on dictionaries.

That’s all for now we’ll continue in the next blog…

Leave a Comment

Your email address will not be published. Required fields are marked *