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 : tup = (1, 2, 3) In : tup Out: (1, 2, 3) # Verifying the type In : type(tup) Out: tuple # Creating a tuple 'tupl' with elements of different data types In : tupl = (1, 'a', 2.5) In : type(tupl) Out: 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 : tup = 1, 2, 3 In : type(tup) Out: tuple
We can repeat a value multiple times within a tuple as follows:
In : tupl = (1,) * 5 # Note trailing comma In : tupl Out: (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 : tup Out: 1 # Access an element at index 2 In : tup Out: 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 : tup Traceback (most recent call last): File "<ipython-input-30-c965c442ca22>", line 1, in <module> tup 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.
len() function is used to check the length of a tuple.
In : len(tup) Out: 3 In : len(tupl) Out: 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 : tup = 10 Traceback (most recent call last): File "<ipython-input-33-991819cff38c>", line 1, in <module> tup = 10 TypeError: 'tuple' object does not support item assignment
As expected, the interpreter threw an error depicting the tuple object to be immutable.
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 : t1 = (1, 2, 3) In : t2 = (4, 5) In : t1 + t2 Out: (1, 2, 3, 4, 5)
Tuples can be concatenated using operators
In : t1 = (1, 2, 3) In : t1 += 4, 5 In : t1 Out: (1, 2, 3, 4, 5)
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 : tup Out: (1, 2, 3) In : x, y, z = tup
The above statement performs the unpacking operation. It will assign the value
1 to the variable
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 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 : tup Out: (1, 2, 3) In : tup.index(3) # Returns the index of value '3'. Out: 2
- count() : This method counts the number of occurrences of a value.
In : tup = (1, 1, 1, 1, 1) In : tup.count(1) Out: 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…