Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Use Multiple Tuple in Python



What is Python Tuple?

A tuple in Python is an ordered collection of items that cannot be changed once defined, making it immutable. A tuple allows duplicate values and can hold elements of different data types, such as strings, numbers, other tuples, and more. This makes tuples useful for grouping related data while the content remains fixed and unchanged.

Multiple Tuples in Python

Multiple tuples organize complex information hierarchically, and these are useful for returning multiple values, iterating over grouped values. In Python, we can create multiple tuples by -

  • Creating Tuple of Tuples

  • Packing and Unpacking Multiple Tuples

  • Concatenating Multiple Tuples

  • Zipping Multiple Tuples

  • Returning MultipleTuples from a Function

Tuple of Tuples

A tuple of tuples in Python is a compound data structure where each element inside the main tuple is itself. This is useful for representing structured, immutable data like rows in a table. 

Example

The Python code defines x as a tuple containing five sub-tuples, each with three elements. The for loop specifies for each sub-tuple into variables a, b, c, then prints their sum. This iterates over all sub-tuples, displays the sum.

x = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14))
for a, b, c in x:
   print(a + b + c)

This will give the output -

3
12
21
30
39

This structure is useful when we need to return a well-defined, ordered arrangement while its immutable.

Packing and Unpacking Multiple Tuples

Packing combines multiple values or tuples into a single tuple using commas or the " * " operator. Unpacking specifies values from tuples into variables. 

Example

This Python code defines x1 and x2 as tuples. The * operator determines their values into variables a,b,c,d. The print() function outputs these values sequentially.

x1 = (3, 4)
x2 = (2, 1)
a, b, c, d = (*x1, *x2)
print(a, b, c, d)

The result is obtained as follows -

3 4 2 1

Concatenating Multiple Tuples

Concatenating multiple tuples in Python means joining two or more tuples into one using the + operator. This creates a new tuple containing all elements from the originals, preserving their order.

Example

This code defines x1 and x2 as tuples. The + operator concatenates them, creating a new tuple x3 with elements (2, 1, 4, 3). The print(x3) statement then outputs this combined tuple, maintaining the original order of elements.

x1 = (2, 1)
x2 = (4, 3)
x3 = x1 + x2
print(x3)

We will get the result as follows -

(2, 1, 4, 3)

Zipping Multiple Tuples

Zipping multiple tuples in Python combines elements from each tuple into pairs or groups, creating an iterator of tuples. It is useful for iterating over related data in parallel, liking pairing names with ages or specified labels.

Example

Here, we are defining two tuples: names and ages. The zip() function pairs corresponding elements from both tuples. The for loop iterates over these pairs, printing each name with its specified age.

names = ("Sai", "Boby")
ages = (40, 35)
for name, age in zip(names, ages):
    print(f"{name} is {age}")

The output is obtained as follows -

Sai is 40
Boby is 35

Returning MultipleTuples from a Function

Returning multiple tuples from a function in Python means the function's output contains more than one tuple at once, a larger tuple contains smaller tuples. This allows structured data to be returned and unpacked into multiple variables.

Example

The function get_pairs(), defines two tuples. The function's output is unpacked into x and y, assigning x = (2, 1) and y= (4, 3). The print() statements display these tuples.

def get_pairs():
    return (2, 1), (4, 3)

x, y = get_pairs()
print(x)  
print(y)

The output is determined as follows -

(2, 1)
(4, 3)
Updated on: 2025-04-24T18:52:18+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements