Python Tuples
Python Tuples
Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar
to lists since the value of the items stored in the list can be changed, whereas the tuple is
immutable, and the value of the items stored in the tuple cannot be changed.
Creating a tuple
A tuple can be written as the collection of comma-separated (,) values enclosed with the
small () brackets. The parentheses are optional but it is good practice to use. A tuple can be
defined as follows.
T3 = 10,20,30,40,50
print(type(T1))
print(type(T2))
print(type(T3))
Output:
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
Note: The tuple which is created without using parentheses is also known as tuple packing.
T4 = ()
Creating a tuple with single element is slightly different. We will need to put comma after the element to
declare the tuple.
tup1 = ("Python")
print(type(tup1))
print(type(tup2))
Output:
<class 'str'>
<class 'tuple'>
A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their
specific index value.
Example - 1
print(tuple1)
count = 0
for i in tuple1:
count = count+1
Output:
tuple1[0] = 10
tuple1[1] = 20
tuple1[2] = 30
tuple1[3] = 40
tuple1[4] = 50
tuple1[5] = 60
Example – 2
print(tuple1)
count = 0
for i in tuple1:
Output:
tuple1[0] = 1
tuple1[1] = 2
tuple1[2] = 3
tuple1[3] = 4
tuple1[4] = 5
tuple1[5] = 6
A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by
using their specific index value.
We will see all these aspects of tuple in this section of the tutorial.
The items in the tuple can be accessed by using the index [] operator. Python also allows us
to use the colon operator to access multiple items in the tuple.
Consider the following image to understand the indexing and slicing in detail.
Consider the following example:
tup = (1,2,3,4,5,6,7)
print(tup[0])
print(tup[1])
print(tup[2])
print(tup[8])
Output:
In the above code, the tuple has 7 elements which denote 0 to 6. We tried to access an
element outside of tuple that raised an IndexError.
tuple = (1,2,3,4,5,6,7)
#element 1 to end
print(tuple[1:])
#element 0 to 3 element
print(tuple[:4])
#element 1 to 4 element
print(tuple[1:5])
print(tuple[0:6:2])
Output:
(2, 3, 4, 5, 6, 7)
(1, 2, 3, 4)
(1, 2, 3, 4)
(1, 3, 5)
Negative Indexing
The tuple element can also access by using negative indexing. The index of -1 denotes the
rightmost element and -2 to the second last item and so on.
The elements from left to right are traversed using the negative indexing. Consider the
following example:
tuple1 = (1, 2, 3, 4, 5)
print(tuple1[-1])
print(tuple1[-4])
print(tuple1[-3:-1])
print(tuple1[:-1])
print(tuple1[-2:])
Output:
(3, 4)
(1, 2, 3, 4)
(4, 5)
Deleting Tuple
Unlike lists, the tuple items cannot be deleted by using the del keyword as tuples are
immutable. To delete an entire tuple, we can use the del keyword with the tuple name.
print(tuple1)
del tuple1[0]
print(tuple1)
del tuple1
print(tuple1)
Output:
(1, 2, 3, 4, 5, 6)
print(tuple1)
Membership It returns true if a particular item exists in print (2 in T1) prints True.
the tuple otherwise false
Iteration The for loop is used to iterate over the tuple for i in T1:
elements. print(i)
Output
1
2
3
4
5
SN Function Description
1 cmp(tuple1, tuple2) It compares two tuples and returns true if tuple1 is greater than tuple2
otherwise false.
print(thistuple)
Output
Example
Print the second item in the tuple:
print(thistuple[1])
Output
Banana
Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last
item etc.
Example
Print the last item of the tuple:
print(thistuple[-1])
Output
Cherry
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
Example
Return the third, fourth, and fifth item:
print(thistuple[2:5])
Output
Note: The search will start at index 2 (included) and end at index 5 (not included).
Example
This example returns the items from index -4 (included) to index -1 (excluded)
print(thistuple[-4:-1])
#This example returns the items from index -4 (included) to index -1 (excluded)
Output
But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back
into a tuple.
Example
Convert the tuple into a list to be able to change it:
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Output
You can loop through the tuple items by using a for loop.
Example
Iterate through the items and print the values:
for x in thistuple:
print(x)
Output
apple
banana
cherry
Check if Item Exists
To determine if a specified item is present in a tuple use the in keyword:
Example
Check if "apple" is present in the tuple:
if "apple" in thistuple:
Output
Add Items
Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
Example
You cannot add items to a tuple:
print(thistuple)
Output
Example
Join two tuples:
tuple2 = (1, 2, 3)
print(tuple3)
Output
Example
Using the tuple() method to make a tuple:
print(thistuple)
Output
Tuple Methods
Python has two built-in methods that you can use on tuples.
Method Description
index() Searches the tuple for a specified value and returns the position of where it was found
Where use tuple?
Using tuple instead of list is used in the following scenario.
1. Using tuple instead of list gives us a clear idea that tuple data is constant and must not
be changed.
2. Tuple can simulate a dictionary without keys. Consider the following nested structure,
which can be used as a dictionary
SN List Tuple
1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
3 The List has the a variable length. The tuple has the fixed length.
4 The list provides more functionality than a The tuple provides less functionality than the list.
tuple.
5 The list is used in the scenario in which we The tuple is used in the cases where we need to store
need to store the simple collections with no
the read-only collections i.e., the value of the items
constraints where the value of the items can
be changed. cannot be changed. It can be used as the key inside
the dictionary.
6 The lists are less memory efficient than a The tuples are more memory efficient because
tuple.
of its immutability.