Skip to content

Computer Science · Ch 10 — Tuples and Dictionaries

Tuple Methods and Built-in Functions

10.3

Tuple Methods and Built-in Functions

Python ships with several ready-made functions and methods for working with tuples. Table 10.1 of the textbook collects the commonly used ones; each is explained below with its behaviour and a worked example.

Method / FunctionWhat it does
len()Returns the number of elements in the tuple
tuple()Creates an empty tuple, or builds a tuple from any sequence
count()Counts how many times a given element appears
index()Returns the index of the first occurrence of an element
sorted()Returns a new sorted list of the tuple's elements
min(), max(), sum()Smallest element, largest element, total of the elements

len() — length of the tuple

Returns the length, i.e. the number of elements of the tuple passed as the argument:

>>> t = (10, 20, 30, 40, 50)
>>> len(t)
5

tuple() — creating tuples

Called with no argument, it creates an empty tuple. Called with a sequence (string, list, range, ...), it converts that sequence into a tuple:

>>> empty = tuple()
>>> empty
()

>>> vowels = tuple('aeiou')     # from a string
>>> vowels
('a', 'e', 'i', 'o', 'u')

>>> nums = tuple([1, 2, 3])     # from a list
>>> nums
(1, 2, 3)

>>> first5 = tuple(range(5))    # from a range
>>> first5
(0, 1, 2, 3, 4)

count() — occurrences of an element

Returns the number of times the given element appears in the tuple. If the element is absent, the count is simply 0 (no error):

>>> t = (10, 20, 30, 10, 40, 10, 50)
>>> t.count(10)
3
>>> t.count(90)
0

index() — position of the first occurrence

Returns the index of the first occurrence of the element. Unlike count(), asking for an element that is not present raises a ValueError:

>>> t = (10, 20, 30, 40, 50)
>>> t.index(30)
2
>>> t.index(90)
ValueError: tuple.index(x): x not in tuple

sorted() — a sorted list of the elements

Takes the elements of the tuple and returns a new sorted list. Two things to remember: the result is a list, not a tuple, and sorted() makes no change to the original tuple:

>>> names = ('Kiran', 'Asha', 'Zara', 'Dev', 'Meera') …
Table 10.1Built-in functions and methods for tuples
MethodDescriptionExample
len()Returns the length or the number of elements of the tuple passed as the argument>>> tuple1 = (10,20,30,40,50)
>>> len(tuple1)
5
tuple()Creates an empty tuple if no argument is passed
Creates a tuple if a sequence is passed as argument
>>> tuple1 = tuple()
>>> tuple1
( )
>>> tuple1 = tuple('aeiou')#string
>>> tuple1
('a', 'e', 'i', 'o', 'u')
>>> tuple2 = tuple([1,2,3]) #list
>>> tuple2
(1, 2, 3)
>>> tuple3 = tuple(range(5))
>>> tuple3
(0, 1, 2, 3, 4)
count()Returns the number of times the given element appears in the tuple>>> tuple1 = (10,20,30,10,40,10,50)
>>> tuple1.count(10)
3
>>> tuple1.count(90)
0
index()Returns the index of the first occurrence of the element in the given tuple>>> tuple1 = (10,20,30,40,50)
>>> tuple1.index(30)
2
>>> tuple1.index(90)
ValueError: tuple.index(x): x not in tuple
sorted()Takes elements in the tuple and returns a new sorted list. It should be noted that, sorted() does not make any change to the original tuple>>> tuple1 = ("Rama","Heena","Raj", "Mohsin","Aditya")
>>> sorted(tuple1)
['Aditya', 'Heena', 'Mohsin', 'Raj', 'Rama']