Python Tuples
Python Tuples
A tuple in Python is similar to a list. The difference between the two is that
we cannot change the elements of a tuple once it is assigned whereas we
can change the elements of a list.
L=(3,4,’a’,”alok”)
Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses () ,
separated by commas. The parentheses are optional, however, it is a good
practice to use them.
A tuple can have any number of items and they may be of different types
(integer, float, list, string, etc.).
# Different types of tuples
# Empty tuple
my_tuple = ()
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
tup=tuple()
Output
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
print(a) # 3
print(b) # 4.6
print(c) # dog
Output
(3, 4.6, 'dog')
3
4.6
dog
Creating a tuple with one element is a bit tricky. Having one element within
parentheses is not enough. We will need a trailing comma to indicate that it
is, in fact, a tuple.
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>
# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>
Output
<class 'str'>
<class 'tuple'>
<class 'tuple'>
T1=tuple(‘hello’)
print(T1) # ('h', 'e', 'l', 'l', 'o')
L=[2,3,4,5]
t=tuple(L)
print(t) # (2, 3, 4, 5)
1. Indexing
We can use the index operator [] to access an item in a tuple, where the
index starts from 0.
So, a tuple having 6 elements will have indices from 0 to 5. Trying to
access an index outside of the tuple index range(6,7,... in this example) will
raise an IndexError .
The index must be an integer, so we cannot use float or other types. This
will result in TypeError .
Likewise, nested tuples are accessed using nested indexing, as shown in
the example below.
print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'
# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
# nested index
print(n_tuple[0][3]) # 's'
print(n_tuple[1][1]) # 4
Output
p
t
s
4
2. Negative Indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
# Output: 't'
print(my_tuple[-1])
# Output: 'p'
print(my_tuple[-6])
Output
t
p
3. Slicing
We can access a range of items in a tuple by using the slicing operator
colon : .
# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')
Output
('r', 'o', 'g')
('p', 'r')
('i', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
Changing a Tuple
Unlike lists, tuples are immutable.
This means that elements of a tuple cannot be changed once they have
been assigned. But, if the element is itself a mutable data type like list, its
nested items can be changed.
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)
Output
(4, 2, 3, [9, 5])
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
t1=(1, 2, 3)
t2=(11, 22, 33)
x=t1+t2 #(1,2,3,11,22,33)
# Repeat
# Output: ('Repeat', 'Repeat', 'Repeat')
print(("Repeat",1) * 3)
Output
(1, 2, 3, 4, 5, 6)
('Repeat',1, 'Repeat',1, 'Repeat',1)
Deleting a Tuple
As discussed above, we cannot change the elements in a tuple. It means
that we cannot delete or remove items from a tuple.
Output
Traceback (most recent call last):
File "<string>", line 12, in <module>
NameError: name 'my_tuple' is not defined
Tuple Methods
Methods that add items or remove items are not available with tuple. Only
the following methods are available.
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
print(max(my_tuple)) # Output: p
print(min(my_tuple)) # Output: a
Output
5
2
3
P
a
# Not in operation
print('g' not in my_tuple)
Output
True
False
True
Output
Hello John
Hello Kate
2 4 8 7 9 12 4
• We generally use tuples for heterogeneous (different) data types and lists for
homogeneous (similar) data types.
• Since tuples are immutable, iterating through a tuple is faster than with list. So there
is a slight performance boost.
• Tuples that contain immutable elements can be used as a key for a dictionary. With
lists, this is not possible.
• If you have data that doesn't change, implementing it as tuple will guarantee that it
remains write-protected.