Javatpoint Logo
Javatpoint Logo

Nested Tuples in Python

A nested tuple is a Python tuple that has been placed inside of another tuple. Let's have a look at the following 8-element tuple.

This last element, which consists of three items enclosed in parenthesis, is known as a nested tuple since it is contained inside another tuple. The name of the main tuple with the index value, tuple[index], can be used to obtain the nested tuple, and we can access each item of the nested tuple by using tuple[index-1][index-2].

Example of a Nested Tuple

Code

Output:

((10, 'Itika', 13000),)
((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395))

Some Operations of Nested Tuples

We will see two necessary operations of nested tuples.

Concatenating Tuples to Nested Tuples

When dealing with tuples, it's occasionally necessary to turn separate records into a nested group while keeping them as independent elements. Tuples are often added by adding the contents, which flattens the resulting container, which is typically undesirable. Let's talk about some approaches to solving this issue.

Using the + operator and "," during initialization

In this technique, we add tuple members as we do, but when initializing tuples, we append a comma after each tuple to prevent flattening during addition.

Code

Output:

Tuple 1 : ((5, 4),)
Tuple 2 : ((1, 6),)
The nested tuple : ((5, 4), (1, 6))

Using the "," Operator

This task can be performed by applying the "," operator during concatenation. It can perform safe concatenation.

Code

Output:

Tuple 1 : (5, 4)
Tuple 2 : (1, 6)
The nested tuple ((5, 4), (1, 6))

Sorting Nested Tuples

We can use the sorted() method to sort a given tuple. By default, this method sorts the tuple in ascending order. For instance, print(sorted(employee)) will arrange the tuple "employee" according to the identification number that appears as the 0th member of all the nested tuples. We can use a lambda function to sort our tuple depending on the other elements of the nested tuple, like the employee name or the count, which is the first and the second member of the nested tuples: print(sorted(employee, key = lambda x: x[1])).

In this case, the key tells the sorted() function, according to which elements we should sort the tuple. The lambda expression: lambda x: x[1] implies that the key, which is the index one element, should be considered for sorting. We can write the lambda expression as lambda x: x[2] to sort our tuple according to the word count.

Code

Output:

[(10, 'Itika', 13000), (15, 'Naill', 20001), (24, 'Harry', 15294), (40, 'Peter', 16395)]
[(40, 'Peter', 16395), (24, 'Harry', 15294), (15, 'Naill', 20001), (10, 'Itika', 13000)]
[(24, 'Harry', 15294), (10, 'Itika', 13000), (15, 'Naill', 20001), (40, 'Peter', 16395)]
[(40, 'Peter', 16395), (15, 'Naill', 20001), (10, 'Itika', 13000), (24, 'Harry', 15294)]
[(10, 'Itika', 13000), (24, 'Harry', 15294), (40, 'Peter', 16395), (15, 'Naill', 20001)]
[(15, 'Naill', 20001), (40, 'Peter', 16395), (24, 'Harry', 15294), (10, 'Itika', 13000)]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA