Table of Contents



Introduction

Tuples are similar to lists with the difference that they are immutable. Once created, their elements cannot be changed unless a new tuple object is created. Tuples are the obvious choice for a collection of constants that never change throughout the program. It is written as a sequence of comma-separated values enclosed within a pair of parentheses ().

Example

Defining a tuple.

1tupA = (1,2,3,3,4,5,6)
2tupB = (1,2,3,"Python","code")
3print(tupA)
4print(tupB)
(1, 2, 3, 3, 4, 5, 6)
(1, 2, 3, 'Python', 'code')

A nested tuple is tuple that contains other tuples as its elements.

Example

Nested tuple.

1newTup = (tupA, tupB,(2,4,6))
2newTup
((1, 2, 3, 3, 4, 5, 6), (1, 2, 3, 'Python', 'code'), (2, 4, 6))

A tuple can also contain other data structures or types as its elements.

Example

Tuple can contain heterogeneous data.

1newTup1 = (tupA, [1,2,4],"string")
2newTup1
((1, 2, 3, 3, 4, 5, 6), [1, 2, 4], 'string')

Basic Tuple Operations

Just like lists, tuples can also be concatenated with the + operator.

Example

Concatenation of tuples.

1tupA + tupB
(1, 2, 3, 3, 4, 5, 6, 1, 2, 3, 'Python', 'code')

We can obtain the length (number of elements) of a tuple using the len() function.

Example

Length of a tuple.

1len(tupA)
7

A tuple can be repeated using the * operator.

Example

Repetition of a tuple.

1tupB*2
(1, 2, 3, 'Python', 'code', 1, 2, 3, 'Python', 'code')

The in keyword serves two purposes:

  • used to check if a value is present in a sequence (e.g. list, tuple).
  • used to iterate through a sequence in a for loop.

Example

Boolean expression with a tuple.

1"Python" in tupB # Is "Python" an element of tupB?
True

ADVERTISEMENT



Tuple Indexing and Slicing

Just like lists, tuples are ordered which means their elements can be accessed by specifying their corresponding indices.

Example

Indexing of a tuple.

1tupA[1]
2

Due to its immutability, trying to modify a tuple will lead to an error.

Example

Tuple is immutable.

1tupA[1] = 20
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_6652/2988985137.py in <module>
----> 1 tupA[1] = 20


TypeError: 'tuple' object does not support item assignment

Instead, create a new tuple object by using a new assignment statement.

1tupA = (1,20,3,3,4,5,6)
2tupA
(1, 20, 3, 3, 4, 5, 6)
Tip

An alternative way to modify a tuple is to convert the original tuple to a list, make the modification and convert it back to a tuple.

Example

Modifying a tuple using a list.

1temp = list(tupA)
2temp[1]=20
3tupA_updated = tuple(temp)
4tupA_updated
(1, 20, 3, 3, 4, 5, 6)
Caution

In the above example, we could have used the original variable name in the assignment statement tupA = tuple(temp). The implication is that the variable name tupA now points to the new tuple object and no longer refers to the original tuple object i.e. (1, 2, 3, 3, 4, 5, 6).

Slicing for tuples are similar to those for lists. Recall the slicing syntax:

sequence[start:stop:step]

Parameter Description
start Optional (default=0). An integer number specifying at which position to start the slicing.
stop An integer number specifying at which position to end the slicing.
step Optional (default=1). An integer number specifying the step of the slicing.

Python uses half-open intervals: the start index is included while the stop index is not.

Example

Indexing of a tuple.

1tup1 = (2,4,6,8,9,10)
2tup1[0]
2

Example

Negative indexing.

1tup1[-2]
9

Example

Slicing of a tuple.

1tup1[2:]
(6, 8, 9, 10)

Chain indexing and slicing work for a nested tuple.

Example

Indexing of a nested tuple.

1tupA = (1,2,3,3,4,5,6)
2newTup1 = (tupA, [1,2,4],"string")
3newTup1[0]
(1, 2, 3, 3, 4, 5, 6)

Example

Chain indexing and slicing of a nested tuple.

1newTup1[0][2:5]
(3, 3, 4)

ADVERTISEMENT



Tuple Methods

The count() method returns the number of elements with the specified value.

Example

count() method.

1numbers = (1,2,2,2,3,3,4,5,6,6,8)
2numbers.count(3) # counts how many times the number 3 appears
2

The index() method searches the tuple for a specified value and returns its index.

Example

index() method.

1numbers.index(5)
7

In cases where there is more than one occurrence of the specified value, the index() method will only return the first index.

Example

index() method.

1numbers.index(2)
1

In contrast to lists, we see that there are fewer methods available to tuples since they are immutable.

Method Description
count() Returns the number of elements with the specified value.
index() Returns the index of the first element with the specified value.