Tuples#

A tuple is a collection of different data types which is ordered and unchangeable (immutable). Tuples are written with round brackets, (). Once a tuple is created, we cannot change its values. We cannot use add, insert, remove methods in a tuple because it is not modifiable (mutable). Methods related to tuples:

  • tuple(): to create an empty tuple

  • count(): to count the number of a specified item in a tuple

  • index(): to find the index of a specified item in a tuple

  • operator: to join two or more tuples and to create a new tuple

Creating a Tuple#

empty_tuple = ()
empty_tuple
()
empty_tuple = tuple()
empty_tuple
()
tpl = ("item1", "item2", "item3")
tpl
('item1', 'item2', 'item3')
fruits = ("banana", "orange", "mango", "lemon")
fruits
('banana', 'orange', 'mango', 'lemon')

Tuple length#

tpl = ("item1", "item2", "item3")
len(tpl)
3

Accessing Tuple Items#

Positive indexing#

Similar to the list data type we use positive or negative indexing to access tuple items.

tpl = ("item1", "item2", "item3")

first_item = tpl[0]
print(first_item)

second_item = tpl[1]
print(second_item)
item1
item2
fruits = ("banana", "orange", "mango", "lemon")

first_fruit = fruits[0]
print(first_fruit)

second_fruit = fruits[1]
print(second_fruit)

last_index = len(fruits) - 1
print(last_index)

last_fruit = fruits[last_index]
print(last_fruit)
banana
orange
3
lemon

Negative indexing#

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last and the negative of the list/tuple length refers to the first item.

tpl = ("item1", "item2", "item3", "item4")

first_item = tpl[-4]
print(first_item)

second_item = tpl[-3]
print(second_item)
item1
item2
fruits = ("banana", "orange", "mango", "lemon")

first_fruit = fruits[-4]
print(first_fruit)

second_fruit = fruits[-3]
print(second_fruit)

last_fruit = fruits[-1]
print(last_fruit)
banana
orange
lemon

Slicing tuples#

We can slice out a sub-tuple by specifying a range of indexes where to start and where to end in the tuple, the return value will be a new tuple with the specified items.

Range of Positive Indexes#

tpl = ("item1", "item2", "item3", "item4")

all_items = tpl[0:4]
print(all_items)

all_items = tpl[0:]
print(all_items)

middle_two_items = tpl[1:3]
print(middle_two_items)
('item1', 'item2', 'item3', 'item4')
('item1', 'item2', 'item3', 'item4')
('item2', 'item3')
fruits = ("banana", "orange", "mango", "lemon")

all_fruits = fruits[0:4]
print(all_fruits)

all_fruits = fruits[0:]
print(all_fruits)

orange_mango = fruits[1:3]
print(orange_mango)

orange_to_the_rest = fruits[1:]
print(orange_to_the_rest)
('banana', 'orange', 'mango', 'lemon')
('banana', 'orange', 'mango', 'lemon')
('orange', 'mango')
('orange', 'mango', 'lemon')

Range of Negative Indexes#

tpl = ("item1", "item2", "item3", "item4")

all_items = tpl[-4:]
print(all_items)

middle_two_items = tpl[-3:-1]
print(middle_two_items)
('item1', 'item2', 'item3', 'item4')
('item2', 'item3')
fruits = ("banana", "orange", "mango", "lemon")

all_fruits = fruits[-4:]
print(all_fruits)

orange_mango = fruits[-3:-1]
print(orange_mango)

orange_to_the_rest = fruits[-3:]
print(orange_to_the_rest)
('banana', 'orange', 'mango', 'lemon')
('orange', 'mango')
('orange', 'mango', 'lemon')

Changing Tuples to Lists#

We can change tuples to lists and vice verse. Tuple is immutable if we want to modify a tuple we should change it to a list.

tpl = ("item1", "item2", "item3", "item4")
lst = list(tpl)
lst
['item1', 'item2', 'item3', 'item4']
fruits = ("banana", "orange", "mango", "lemon")
fruits = list(fruits)
fruits[0] = "apple"
print(fruits)

fruits = tuple(fruits)
print(fruits)
['apple', 'orange', 'mango', 'lemon']
('apple', 'orange', 'mango', 'lemon')

Checking an Item in a Tuple#

We can check if an item exists or not in a tuple using in, it returns a boolean.

tpl = ("item1", "item2", "item3", "item4")
"item2" in tpl
True
fruits = ("banana", "orange", "mango", "lemon")
print("orange" in fruits)  # True
print("apple" in fruits)  # False
True
False
# fruits[0] = 'apple' # Will raise exception: TypeError: 'tuple' object does not support item assignment

Joining Tuples#

We can join two or more tuples using + operator.

tpl1 = ("item1", "item2", "item3")
tpl2 = ("item4", "item5", "item6")
tpl3 = tpl1 + tpl2
tpl3
('item1', 'item2', 'item3', 'item4', 'item5', 'item6')
fruits = ("banana", "orange", "mango", "lemon")
vegetables = ("Tomato", "Potato", "Cabbage", "Onion", "Carrot")
fruits_and_vegetables = fruits + vegetables
fruits_and_vegetables
('banana',
 'orange',
 'mango',
 'lemon',
 'Tomato',
 'Potato',
 'Cabbage',
 'Onion',
 'Carrot')

Deleting Tuples#

It is not possible to remove a single item in a tuple but it is possible to delete the tuple itself using del.

tpl1 = ("item1", "item2", "item3")
del tpl1
fruits = ("banana", "orange", "mango", "lemon")
del fruits