Python Tuples

Python Tuple is a collection of items like list but it’s item are unchangeable or immutable. So, after creating a Tuple, we cannot add, delete or modify any item in it.

Creating a Tuple

We use round brackets to create a Tuple.

Example:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
print('Our tuple is: ', sports)

Output:
Our tuple is:  ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')

We also can use tuple() constructor to create the tuple.

Example:

sports1 = tuple(('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby'))
print('Our tuple is: ', sports1)

Output:
Our tuple is:  ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')

Note: If we want to use tuple() constructor, we have to use double round brackets

Accessing  Tuple elements 

If we have n numbers of elements in a tuple, index/position of the first element will be 0 and last element will be n-1.

So, if we want to get the first sport name, we can simply use sports[0] . Let us print the first sport name:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
print('Item in the first position is: ', sports[0])

Output:
Item in the first position is:  Cricket

Print the second sport name:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
print('Item in the second position is: ', sports[1])

Output:
Item in the second position is:  Football

Print the last or 5th sport name:


sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
print('Item in the 5th position is: ', sports[4])

Output:
Item in the 5th position is:  Rugby

Find Index of a particular tuple item:

index() method is used to find the index of a particular tuple item

Example:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
indexOfTennis = sports.index('Tennis')
print('Index of Tennis is: ', indexOfTennis)

Output:
Index of Tennis is:  2

Finding the length of the tuple:

To find the number of elements in a tuple or the length of the tuple, we can use len() method.

Example:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
lengthSportsTuple = len(sports)
print('Length of the sports tuple is: ',lengthSportsTuple)

Output:
Length of the sports tuple is:  5

Looping through a Tuple

Looping through a tuple is a very common operation to access the items in it. We will learn more about looping while studying different types of looping.
Example: In this example we are going to fetch all the tuple elements using for loop

for item in sports:
   print(item)

Output:
Cricket
Football
Tennis
Baseball
Rugby

Check if an item is available in a tuple or not

We can use if condition with in keyword to check the availability of an item in the tuple.

Example:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
if "Cricket" in sports:
 print("Cricket is available in the sports tuple")
else:
   print("Cricket is not available in the sports tuple")

Output:
Cricket is available in the sports tuple

Count Tuple element:

If you want to count how many times a tuple element exists in the tuple, you can use count() function

Example:

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
numTime = sports.count('Cricket')
print('Number of times Cricket available in the tuple is: ',numTime)


Output:
Number of times Cricket available in the tuple is:  1

Note: as already mentioned above that  adding, deleting and changing item is not possible in a tuple, let’s see what happen if we want to add a new item in the tuple.

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
sports.append('Skating')

Output:
AttributeError: 'tuple' object has no attribute 'append'

Delete the entire tuple
We can delete the entire tuple using del keyword

sports = ('Cricket', 'Football', 'Tennis', 'Baseball', 'Rugby')
del sports
print(sports)

Output:
NameError: name 'sports' is not defined

In summary, commonly used Python Tuple methods are:

MethodDescription
index()finds the index of a particular tuple item
len()finds the number of elements in a tuple or the length of the tuple
count()counts how many times a tuple element exists in the tuple