Javaexercise.com

Creating Nested Tuples in Python

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry.

One of the most important concepts in python is that of nested tuples. Let us have a look at it in detail.

What are tuples in Python?

In python, tuples are the data structures that programmers use to store data of different types.

Tuples are built-in python data types that are used for the storage of collections of items.

A tuple in python is very much like a list, although there are a few significant differences.

A tuple contains data in an ordered manner.

Also, the data stored in tuples is not changeable, unlike the data stored in lists.

It allows duplicate elements to store.

Another syntactical difference is that unlike lists in python that use square brackets, tuples use round brackets to enclose comma-separated data items as shown below:

<tuple_name> = (<item1>, <item2>, ….)

Here, we have an example of a tuple named a containing three data items, 0, 1, and 2.

Let us look at the python code and corresponding output for this:

# Defining the tuple
a = (0,1,2)

# Printing the tuple
print(a)

Output

(0, 1, 2)  

What are nested tuples in Python?

In python, you can have nested tuples as well.

This refers to the structure that has a tuple as a data item of another outer tuple.

The normal rules of tuples in python apply to this concept as well.

Here, we have an example of such a nested tuple. We have a nested tuple named a that contains two data items.

Each of these data items is a tuple of length two here.

The first one contains two data items 0 and 1 whereas the second one contains two data items 2 and 3.

Let us have a look at the python code and output for this example:

# Defining a nested tuple
a = ((0,1),(2,3))

# Printing the tuple
print(a)

Output

((0, 1), (2, 3))

Accessing data items of nested tuples in Python

In this example, we look at the method of accessing data items in such nested tuples.

The method is the same as what would be used in the case of a normal tuple, that is, using square brackets with the required index.

Let us say we want to access the second data item of this nested tuple, which happens to be a tuple (2, 3) in this case.

We use square brackets with the index 1, i.e. [1] for this scenario.

Remember that indices in python are zero-based.

# Defining a nested tuple
a = ((0,1),(2,3))

# Printing an item
print(a[1])

Output

(2, 3)

Now, if we want to access the data items inside this inner tuple, we use square brackets with indices once more.

Let us say we want to access the second element of the second tuple in the outer tuple, which happens to be 3.

We use square brackets with the index 1 for this task, i.e. [1]. Thus the expression a[1][1] can be used to access this element here.

# Defining a nested tuple
a = ((0,1),(2,3))

# Printing an item
print(a[1][1])

Output

Nested tuples are not changeable in Python

As mentioned earlier, the rules of normal tuples apply to nested tuples in python as well.

This means that nested tuples are also not changeable.

Let us consider the tuple we have used for previous examples and try to change the second element of the first inner tuple here from 1 to 5.

We receive an error on trying to perform such an operation because it is not allowed in python. See the python code below.

# Defining a nested tuple
a = ((0,1),(2,3))

print(a)

# Trying to change a data item
a[0][1] = 5

Output

((0, 1), (2, 3))

TypeError: 'tuple' object does not support item assignment

 

Traversing nested elements using a for loop

In this example, we take a look at traversing nested elements of a nested tuple using a for loop, a structure that is traditionally used to loop over iterables. Since we have two levels of nesting here, meaning that there is an outer tuple layer and an inner tuple layer, we need two levels of nested for loop as well. First we define the tuple a containing two tuples (0,1) and (2,3). The outer loop iterates over the outer tuple whereas the inner loop iterates over the inner tuples, i.e. prints each element inside the current inner tuple being iterated over. Let us take a look at the python code and corresponding output for this method - 

# Defining the tuple

a = ((0,1),(2,3))

# Outer loop for the tuple

for i in a:

  # Inner loop for the tuple

  for j in i:

    # Printing an element

    print(j)

Output

0

1

2

3

Conclusion

In this topic, we have learned the use and advantages of nested tuples in a Python program, following some simple running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.