TUPLE
TUPLE
Tuple
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store
collections of data, the other 3 are List, Set, and Dictionary,
all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
Example
Create a Tuple:
thistuple = ("apple" , "banana" , "cherry" )
print (thistuple)
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate
values.
Tuple items are indexed, the first item has index [0] , the
second item has index[1] etc.
Ordered
When we say that tuples are ordered, it means that the items
have a defined order, and that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or
remove items after the tuple has been created.
Allow Duplicates
Since tuple are indexed, tuples can have items with the same
value:
Example
Tuples allow duplicate values:
thistuple = ("apple" , "banana" , "cherry" , "apple" ,"cherry" )
print (thistuple)
Tuple Length
To determine how many items a tuple has, use the len( )function:
Example
Print the number of items in the tuple:
thistuple = ("apple" , "banana" , "cherry" )
print (len (thistuple))
type()
From Python's perspective, tuples are defined as objects with
the data type 'tuple':
<class 'tuple'>
Example
What is the data type of a tuple?
mytuple = ("apple" , "banana" , "cherry" )
print (type (mytuple))
Range of Indexes
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.
Example
Return the third, fourth, and fifth item:
thistuple = ("apple" , "banana" , "cherry" , "orange" , "kiwi" ,
"melon" , "mango" )
print (thistuple[2 :5 ])
Note: The search will start at index 2 (included) and end at
index 5 (not included).
Remember that the first item has index 0.
By leaving out the start value, the range will start at the
first item:
Example
This example returns the items from the beginning to, but NOT
included, "kiwi":
thistuple = ("apple" , "banana" , "cherry" , "orange" , "kiwi" ,
"melon" , "mango" )
print (thistuple[:4 ])
By leaving out the end value, the range will go on to the end of
the list:
Example
This example returns the items from "cherry" and to the end:
thistuple = ("apple" , "banana" , "cherry" , "orange" , "kiwi" ,
"melon" , "mango" )
print (thistuple[2 :])
Add Items
Once a tuple is created, you cannot add items to it.
Example
You cannot add items to a tuple:
thistuple = ("apple" , "banana" , "cherry" )
thistuple.append("orange" ) # This will raise an error
print (thistuple)
Just like the workaround for changing a tuple, you can convert
it into a list, add your item(s), and convert it back into a
tuple.
Example
Convert the tuple into a list, add "orange", and convert it back
into a tuple:
thistuple = ("apple" , "banana" , "cherry" )
y = list (thistuple)
y.append("orange" )
thistuple = tuple (y)
Remove Items
Note: You cannot remove items in a tuple.
Tuples are unchangeable, so you cannot remove items from it, but
you can use the same workaround as we used for changing and
adding tuple items:
Example
Convert the tuple into a list, remove "apple", and convert it
back into a tuple:
thistuple = ("apple" , "banana" , "cherry" )
y = list (thistuple)
y.remove("apple" )
thistuple = tuple (y)
Or you can delete the tuple completely:
Example
The de l keyword can delete the tuple completely:
thistuple = ("apple" , "banana" , "cherry" )
del thistuple
print (thistuple) #this will raise an error because the tuple no
longer exists
Python - Unpack Tuples
Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is
called "packing" a tuple:
Example
Packing a tuple:
fruits = ("apple" , "banana" , "cherry" )
But, in Python, we are also allowed to extract the values back
into variables.This is called "unpacking":
Example
Unpacking a tuple:
fruits = ("apple" , "banana" , "cherry" )
(green, yellow, red) = fruits
print (green)
print (yellow)
print (red)
Note: The number of variables must match the number of values in
the tuple, if not, you must use an asterix to collect the
remaining values as a list.
Using Asterix *
If the number of variables is less than the number of values,
you can add an * to the variable name and the values will be
assigned to the variable as a list:
Example
Assign the rest of the values as a list called "red":
fruits = ("apple" , "banana" , "cherry" , "strawberry" ,
"raspberry" )
(green, yellow, *red) = fruits
print (green)
print (yellow)
print (red)
If the asterix is added to another variable name than the last,
Python will assign values to the variable until the number of
values left matches the number of variables left.
Example
Add a list of values the "tropic" variable:
fruits = ("apple" , "mango" , "papaya" , "pineapple" ,"cherry" )
green, *tropic, red) = fruits
print (green)
print (tropic)
print (red)
Python - Loop Tuples
Loop Through a Tuple
You can loop through the tuple items by using a fo r loop.
Example
Iterate through the items and print the values:
thistuple = ("apple" , "banana" , "cherry" )
for x in thistuple:
print (x)
Loop Through the Index Numbers
You can also loop through the tuple items by referring to their
index number.
Use the range( ) and len( ) functions to create a suitable
iterable.
Example
Print all items by referring to their index number:
thistuple = ("apple" , "banana" , "cherry" )
for i in range (len (thistuple)):
print (thistuple[i])
Multiply Tuples
If you want to multiply the content of a tuple a given number of
times, you can
use the * operator:
Example
Multiply the fruits tuple by 2:
fruits = ("apple" , "banana" , "cherry" )
mytuple = fruits * 2
print (mytuple)
Python - Tuple Methods
Tuple Methods
Python has two built-in methods that you can use on tuples.
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