Tuple in Python

Tuple is a data structure in Python which is an immutable , ordered collection of objects.
Tuples are faster and consume less memory.

Create an empty tuple
t = ()
t = tuple()

Create a tuple with single element
t = tuple(1,)
t = 1,

Note : if you are creating a tuple with single element, you need to specify a comma in the end,

Create a tuple with multiple elements
t = ('Raj','23-01-2009','Male','Pune')
('Raj', '23-01-2009', 'Male', 'Pune')

Access individual element of a tuple
Elements in a tuple are indexed, with index starting from 0
So, to access first element we would say
Syntax: tuple[index]

We can also use negative index.
If we give negative index then tuple is accesses from end
for example index = -1 will retrieve last element

We can also access a range of elements of a tuple
Syntax: tuple[start_index, end_index],
where end_ index is exclusive.
Lets say tuple is
rainbow = ('V','I','B','G','Y','O','R')
We can access a range of elements as
('V', 'I', 'B', 'G')

In above example 0 is start index, 4 is end index and exclusive
i.e it will access elements starting from index 0 to index 3

We can also specify step to jump indexes as following
Syntax: tuple[start_index, end_index, step]
('V', 'B', 'Y')

It will access from 0 to index 5 , at step i.e jump of 2,
so it will retrieve elements for index 0, 2, 4

One cool trick to reverse elements of a tuple is
('R', 'O', 'Y', 'G', 'B', 'I', 'V')

Lenth of a tuple: i.e no. of elements in a tuple

To find index of an element
Syntax: tuple.index(element)

If element is not present in tuple, it gives error.

Count the occurance of element in tuple
Syntax: tuple.count(element)

nums = (1, 2, 3, 4, 3, 2, 4, 5, 3, 2)

Tuple unpacking
We can unpack a tuple and assign elements to variables
Say a tuple is :
traffic_colors = (‘Red’, ‘Green’, ‘Yellow’)
stop, go, ready = traffic_colors
print("stop:{}, go:{}, ready:{}".format(stop, go, ready))
stop:Red, go:Green, ready:Yellow

Even though tuple is immutable, however if it contains a mutable element , then we can change it
Say we have a tuple which captures min , max temperature and list of cities which had those temperature
city_temperature = (16, 27, ['Pune', 'Mumbai'])

To note, list is mutable object which is present in tuple, so we can modify it
Say Nagpur and Nasik to be added to list element of tuple
city_temprature[2].extend(['Nagpur', 'Nasik'])
(16, 27, ['Pune', 'Mumbai', 'Nagpur', 'Nasik'])

Use Case :
tuple as a key, pair, forming keys of a dictionary
chemical_elements = (('C', 12), ('O', 16), ('N', 14))
(('C', 12), ('O', 16), ('N', 14))

chemical_elements is a tuple of tuples, i.e each element is a tuple itself having chemical element and its atomic mass.

We can convert this to a dictionacy as :
atomic_mass = dict(chemical_elements)
{'C': 12, 'O': 16, 'N': 14}

One can easily access atomic mass of chemical elements, say Carbon as

Shekhar Pandey

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.