Python Tuples

About Python Tuples

In Python, a tuple is a collection of items that are ordered. The items are seperated by comma (,).

Unlike list, Python tuple is immutable (unchangeable). You cannot change its content after it has been created.

A tuple can contain items of different data types.

Creating a tuple

You can create a tuple by enclosing a sequence of elements inside parentheses '()'. Elements of tuple must be seperated by comma (,).

In the following example, var_tuple is a variable of a tuple and (1, "apple", 2.5, [4, 5]) is the sequence of items inside parentheses.

The tuple of the example contains different data types; 1 is integer data type, "apple" is string data type, 2.5 is float data type, and [4, 5] is a list data type.

Example of creating tuple
Output of the above example
 (1, "apple", 2.5, [4, 5])
                                  

Accessing elements of a tuple

In Python, you can access elements of a tuple using indexing. Indexing in Python is 0 based. If want to want to access first element of a tuple, you need to use index 0.

The index is placed after the tuple variable without any space inside square brackets '[]'.

In the following example, num_tuple[0] is used to get the first element of the tuple where the num_tuple is the tuple and (1, 2, 3, 4 ,5 ,6) are the items (elements) of the tuple. num_tuple[2] is used to get the third element of the tuple.

Example of accessing elements of a tuple using indices
Output of the above example
The first element of the tuple: 1
The third element of the tuple: 3
                                

Accessing elements of a tuple using negative indices

In Python, you can also use negative indices to access elements from the end of the tuple.

Index '-1' refers to the last element, '-2' refers to the second last, and so on.

In the following example, num_tuple[-1] is used to get the last element of the tuple. num_tuple[-3] is used to get the third element from the end of the tuple.

Example of accessing elements of a tuple using negative indices:
Output of the above example
The last element of the tuple: 6
The third element from the end of the tuple: 4
                                

Slicing a tuple to create a new tuple

In Python, you can create a new tuple from a subset of elements of an existing tuple.

In the following first example, num_tuple[2:5] refers that the selection will start from the index '2' and before the index '5' of the tuple (element from index 2 to 4 will be included in the tuple while element at index '5' will be excluded. The new tuple will be (3, 4, 5).

In the second example, num_tuple[:4] refers that the selection will start from the beginning up to the index 4 (element from index 0 to 4 will be included in the tuple. The new tuple will be (1, 2, 3, 4, 5).

In the third example, num_tuple[3:] refers that the selection will start after the index 3 to the end of the tuple (element from index 4 to the last index). The new tuple will be (4, 5, 6, 7, 8).

In the fourth example, num_tuple[::2] refers that starting from the first every second element of the tuple will be selected. The new tuple will be (1, 3, 5, 7).

In the fifth example, num_tuple[::-2] refers that starting from the last every second element of the tuple will be selected reversely. The new tuple will be (1, 3, 5, )].

Please have a closer look at the following examples and try the code in our Python editor.

Example of slicing a tuple to create a new tuple:
Output of the above example
Elements from index 2 to 4: (3, 4, 5)
All elements upto index 4: (1, 2, 3, 4, 5)
All elements after index 3: (4, 5, 6, 7, 8)
Every second elements of the tuple: (1, 3, 5, 7)
Every second elements of the tuple reversely: (8, 6, 4, 2)
                                

Iterating through the elements tuple

In Python, you can iterate through the elements of tuples using a for loop.

In the following example, var_tuple is a tuple variable with (1, 2, 3, 4 ,5 , 6) items. for element in var_tuple: is used loop through the tuple variable 'var_tuple'. print(element) prints the elements of tuple.

Example of iterating throgh the elements of a tuple
Output of the above example
1
2
3
4
5
6
                                

Tuple Methods

Since tuples are immutable, there are no methods like append(), remove(), insert(), or pop(). The standard tuple methods available in Python are explained below:

index(): The index() method returns the index of the first occurrence of a specified element in the tuple. In the following example, x_tuple.index(3) is used to get the index of first occurrence of 3 in the tuple.'

count(): The x_tuple.count() method returns the number of occurrences of an element in the tuple. In the following example, x_tuple.count(4) is used to return the number of occurrences of element '4' in the x_tuple.

Example of standard tuple methods
Output of the above example
The index of 3 is: 2
Number of occurrences of element '4' in the tuple is: 3
                                

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python tuples and try different methods and functions on the elements of tuple in the following Python code editor by clicking Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output