Python - Tuples
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.
CREATE A TUPLE :
>>> tuple=("apple","ball","cat","dog")
>>> print(tuple)
('apple', 'ball', 'cat', 'dog')
We can access tuple items by referring to the index number, inside square brackets.
>>>tuple=("apple',"ball","cat","dog")
>>>print(tuple[1])
ball
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
>>>tuple=("apple","banana","cat","dog")
>>>print(tuple[-2])
cat
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.
>>> tuple=("apple","ball","cat","dog","egg","fish")
>>> print(tuple[2:4])
('cat', 'dog')
Note: The search will start at index 2 (included) and end at index 5 (not included).
Specify negative indexes if you want to start the search from the end of the tuple.This returns the items from index -4 (included) to index -1 (excluded).
>>> tuple=("apple","banana","catch","bounce")
>>> print(tuple[-3])
banana
Tuples,being unchnaged and immutable values cant be chnaged, but there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
x = ("apple", "banana", "cherry")
>>>y = list(x)
>>>y[1] = "kiwi"
>>>x = tuple(y)
>>>print(x)
('apple','kiwi','cherry')
We can loop through the tuple items by using a for loop.
tuple=("apple","banana","cat","dog")
for i in tuple:
print(i)
apple
banana
cat
dog
To determine if a specified item is present in a tuple use the in keyword:
tuple=("apple","banana","cat","dog")
if "apple" in tuple:
print("yes,'apple' is in tuple")
yes,'apple' is in tuple
To determine how many items a tuple has, use the len() method.
tuple=("app","camera","google","chrome","uc browser")
print(len(tuple))
5
Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)
Traceback (most recent call last):
File "demo_tuple_add.py", line 2, in <module>
thistuple[3] = "orange" # This will raise an error
TypeError: 'tuple' object does not support item assignment
To create a tuple with only one item, you have to add a comma after the item, unless Python will not recognize the variable as a tuple.
tuple=("apple",)
print(type(tuple))
#not a tuple
tuple=("apple")
print(type(tuple))
<class 'tuple'>
<class 'str'>
Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely using the del keyword.
tuple = ("apple", "banana", "cherry")
del tuple
print(tuple) #this will raise an error because the tuple no longer exists
To join two or more tuples you can use the + operator.
tuple=("apple","ball","cat","dog")
tuple1=(1,2,3,4)
tuple3=tuple+tuple1
print(tuple3)
('apple', 'ball', 'cat', 'dog', 1, 2, 3, 4)
It is also possible to use the tuple() constructor to make a tuple.
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
('apple', 'banana', 'cherry')
Method | Description |
---|---|
count() | Returns the number of times a specified value occurs in a tuple |
index() | Searches the tuple for a specified value and returns the position of where it was found |
Function | Description |
---|---|
cmp(tuple1, tuple2) | It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false. |
len(tuple) | It calculates the length of the tuple. |
max(tuple) | It returns the maximum element of the tuple. |
min(tuple) | It returns the minimum element of the tuple. |
tuple(seq) | It converts the specified sequence to the tuple |