The only difference between a Tuple and a list is that tuples are immutable. Tuples once created cannot be modified.
t = (5,’program’, 1+3j)
print(t[0:3]) ### Output will be (5,’program’, 1+3j)
As we mentioned, tuples are immutable, We can use the slicing operator [] to extract items but we cannot change its value as in example below.
t[0] = 10. # Generates error as Tuples are immutable
my_tuple = (1, 2, 3) # Tuple having integers
my_tuple = (1, “Hello”, 3.4) # tuple with mixed datatypes
my_tuple = (“mouse”, [8, 4, 6], (1, 2, 3)). # nested tuple
my_tuple = 3, 4.6, “dog”
# tuple unpacking is also possible
a, b, c = my_tuple
print(a) # 3
print(b) # 4.6
print(c) # dog
Access Tuple Elements:
Indexing:
- Uses the index operator [] to access an item in a tuple where the index starts from 0.
- The index must be an integer; so we cannot use float or other types. This will result in TypeError.
- Nested tuples are accessed using nested indexing
t = (‘p’,’y’,’t’,’h’,’o’,’n’)
print(t[1]) # Output: ‘y’
t = (“cat”, [8, 4, 6], (1, 2, 3))
print(n_tuple[1][1]) # Output: 4
- The index of -1 refers to the last item, -2 to the second last item and so on.
t = (‘p’,’y’,’t’,’h’,’o’,’n’)
print(t[-6]) # Output: ‘p’
Slicing:
- We can access a range of items in a tuple by using the slicing operator – colon “:”.
t = (‘p’,’y’,’t’,’h’,’o’,’n’)
print(t[1:4])
print(t[:-6])
print(t[5:])
print(t[:])
(‘y’, ‘t’, ‘h’)
()
(‘n’,)
(‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Changing a Tuple:
- As we discussed earlier, unlike lists, tuples are immutable. Hence the elements inside tuple cannot be changed once it has been assigned.
- However, if the element is itself a mutable datatype like list, its nested items can be changed.
- We can also assign a tuple to different values (reassignment).
t = (4, 2, 3, [6, 5])
t[1] = 9
print(t) # Output: # TypeError: ‘tuple’ object does not support item assignment
t[3][0] = 9
print(t) #Output: t = (4, 2, 3, [9, 5]).
- We can use + operator to combine two tuples. This is also called concatenation.
- We can also repeat the elements in a tuple for a given number of times using the *operator.
print((1, 2, 3) + (4, 5, 6))
print((“Repeat”,) * 3)
Deleting a Tuple:
- As discussed earlier in this post, we cannot change the elements in a tuple.
- That also means we cannot delete or remove items from a tuple.
- However, deleting a tuple entirely is possible using the keyword del.
t = (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
del t
Tuple Methods:
- Methods that add items or remove items are not available with tuple.
- Only the following two methods are available.
- Some examples of Python tuple methods:
t = (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
print(t.count(‘p’))
Other Tuple Operations:
- Tuple Membership Test:
- We can test if an item exists in a tuple or not, using the keyword in.
t = (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
print(‘o’ in t)
- Iterating Through a Tuple:
- Using a for loop we can iterate through each item in a tuple.
print(“Hello”,name)
Advantages of Tuple over List:
- We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
- Since tuples are immutable, iterating through 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.