Python Tuple

  • Python

In the previous article, we learned about list and a Python Tuple is similar to a Python List. Here, we use round brackets () instead of square brackets [].

new_tuple = (28, 4, 90)

And the difference between them is that we cannot change values/elements in a tuple just like we can in a List. This means that a Python Tuple is Immutable.

Why did we create a Tuple when we had list?

Suppose you are writing a code and you don’t want to change some values. Tuple comes in action here. As an example, let’s understand this by location coordinates which have to be precise and you don’t want changing these values. By using tuples, since the values cannot be changed, iteration is also fast.

Just like a List, we can add any number of values and any type of values. A Tuple ca also be created without using round brackets but the output will still be a tuple within round brackets. Let’s understand this by some examples.

# Empty Tuple
new_tup = ()

# Tuple with only string values
new_tup = ('a', 'b', 'c')

# Tuple with only integer values
new_tup = (1, 2, 3)

# Tuple with mixed values
new_tup = (1, 2, 3, "Techie", 4.0)

# Create Tuple without round brackets
new_tup = 1, 3, "Techie", 7.0

Output :

()
('a', 'b', 'c')
(1, 2, 3)
(1, 2, 3, "Techie", 4.0)
(1, 3, "Techie", 7.0)

Methods in a Tuple

Just like in List, we have methods for a Python Tuple as well but since it is an Immutable object, we only have two methods count() and index(). Just as the name suggests, count() is used to count how many times an element has occurred in a tuple and index() will give us it’s position as first occurrence. Let’s understand this with an example.

new_tup = (1, 2, 3, 1, 3, 4, 1)

# Count method - Takes only one argument
new_tup.count(1)
new_tup.count(4)


# Index Method
new_tup.index(2)
new_tup.index(4)

Output :

3
1
1
5

In the above example, we learned the working of count() and index() where count() gives the no.of time an element has occurred and index() gives it first positional values.

Accessing and Modifying elements in a Tuple

Accessing an element in a tuple will be similar to accessing an element in a Python List. We use [] to access any specific item and indexing will also start from 0.

new_tup = (1, 2, 3, "Techie", 4.0)

print(new_tup[1])
print(new_tup[3])

Output :

2
"Techie"

You can some time come across a situation where you use a nested tuple. Accessing an element from the nested tuple will be a bit different. Check this example –

new_tup = (1, 2, 3, 4, (5, 6, 7, 8))

# To access 5, we'll write
print(new_tup[4][0])

# Similarly for 8
print(new_tup[4][3])

Output :

5
8

We learned how to access elements in the above example, now what if you want to change elements in a tuple? it is not possible as a tuple is an immutable object. But, if we have an mutable object inside a tuple, it is possible. Let’s understand this with this example.

new_tup = (1, 2, 3, [4, 5], 6)

new_tup[3][1] = 15

print(new_tup)

Output :

(1, 2, 3, [4, 15], 6)

So, in case there’s a mutable inside a tuple, the mutable object can be changed but not any other element. It will give you the following error if you try to perform this.

...
TypeError: 'tuple' object does not support item assignment

Similarly, we also cannot delete any element from a tuple. But, deleting a tuple itself is allowed.

new_tup = (1, 2, 3, "Techie", 4.0)

del new_tup

# After running, if you try to print new_tup
print(new_tup)

Output :

...
NameError: name 'new_tup' is not defined