Python Tuple
Python Tuple
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.
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.
1. tup1 = ("JavaTpoint")
2. print(type(tup1))
3. #Creating a tuple with single element
4. tup2 = ("JavaTpoint",)
5. 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
Output:
Example - 2
Output:
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.
1. tup = (1,2,3,4,5,6,7)
2. print(tup[0])
3. print(tup[1])
4. print(tup[2])
5. # It will give the IndexError
6. print(tup[8])
Output:
1
2
3
tuple index out of range
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.
1. tuple = (1,2,3,4,5,6,7)
2. #element 1 to end
3. print(tuple[1:])
4. #element 0 to 3 element
5. print(tuple[:4])
6. #element 1 to 4 element
7. print(tuple[1:5])
8. # element 0 to 6 and take step of 2
9. 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:
1. tuple1 = (1, 2, 3, 4, 5)
2. print(tuple1[-1])
3. print(tuple1[-4])
4. print(tuple1[-3:-1])
5. print(tuple1[:-1])
6. print(tuple1[-2:])
Output:
5
2
(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.
1. tuple1 = (1, 2, 3, 4, 5, 6)
2. print(tuple1)
3. del tuple1[0]
4. print(tuple1)
5. del tuple1
6. print(tuple1)
Output:
(1, 2, 3, 4, 5, 6)
Traceback (most recent call last):
File "tuple.py", line 4, in <module>
print(tuple1)
NameError: name 'tuple1' is not defined
Repetition The repetition operator enables the tuple elements to T1*2 = (1, 2, 3, 4, 5, 1,
2, 3, 4, 5)
be repeated multiple times.
Concatenation It concatenates the tuple mentioned on either side of T1+T2 = (1, 2, 3, 4, 5, 6,
7, 8, 9)
the operator.
Membership It returns true if a particular item exists in the tuple print (2 in T1) prints
True.
otherwise false
Iteration The for loop is used to iterate over the tuple elements. for i in T1:
print(i)
Output
1
2
3
4
5
SN Function Description
1 cmp(tuple1, It compares two tuples and returns true if tuple1 is greater than tuple2
tuple2) otherwise false.
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
need to store the simple collections with no store the read-only collections i.e., the value of the
constraints where the value of the items can items cannot be changed. It can be used as the key
be changed. inside the dictionary.
6 The lists are less memory efficient than a The tuples are more memory efficient because of its
tuple. immutability.