×

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python.

What is a Tuple?

Multiple items can be stored in a single variable by using tuples. One of the four built-in data types in Python for storing data collections is the tuple; the other three are list, set, and dictionary, each with a unique set of features and applications. A tuple is an unchanging, ordered collection.

A tuple is similar to a list, which helps in storing data in python. The major difference between a tuple and a list is that the list is mutable, but the tuple is immutable. In a simple way, we can say, even after declaring a list, we can change the items of the list, but we cannot change the items in a tuple once it is declared. Hence, tuples are immutable, as said above. To declare a list, we make use of square brackets. Similarly, we make use of the parentheses, aka round brackets, in the declaration of a tuple.

Syntax of declaring a Tuple

tuplename = (var_1, var_2, var_3, var_4, ........, var_n)

Example of declaring a tuple

tup = (29, 18, 2, 30)

Program to print a tuple element

tup = (29, 18, 2, 30)
print(tup[1])

Output

Nested Tuple in Python

Program to explain that the declared tuple cannot be changed

tup = (29, 18, 2, 30)
tup[2] = 19

Output

Nested Tuple in Python

This is the syntax of a tuple containing n items in it. The commas separate the variables from one another, and the parentheses are used to declare the tuple as mentioned above.

Methods like index and count can be used in combination with tuples as we use them in the case of lists, but there is no method to help replace the items of a tuple once it is declared. So, a tuple is used in situations where the user knows that he doesn't need to change the items of the tuple. Since the items in a tuple cannot be changed, this makes the iterations faster in a tuple when compared to lists. So, it means that the tuples are executed faster than lists. The tuples are also indexed similarly to the lists.

Tuples also follow zero-based indexing, starting from its first element having assigned with the index [0]. When we print a tuple, its items are printed in user-defined order, unlike the sets whose elements are printed in random order. Also, duplicate elements are allowed to be printed in a tuple, while sets don’t allow duplicate elements in their outputs in python language.

Program to print a tuple

tuple = (' A ', ' B ', ' C ', ' D ')
print(tuple)

Output

Nested Tuple in Python

Program to print a set

s = {12, 16, 20, 15, 10, 15}
print(s)

Output

Nested Tuple in Python

A tuple's type is defined as the tuple itself while it can store items of different data types such as Boolean, int, and a string. There is a quality in tuples that makes them unique, i.e., a tuple can store items of different data types in it at the same time.

Program of a single tuple declaring both strings and integers

tuple = (" Hello ", 20, " World ", 19)
print(tuple)

Output

Nested Tuple in Python

What are Nested Tuples?

 A tuple having one or more tuples nesting inside it is called a nested tuple. In other words, if a tuple itself contains items that are also tuples, it is known as a nested tuple. To consider an example, let “a = (10, 20, 30, (50, 60))” is a tuple that is declared in python. Here, “a” is called a nested tuple as it contains another tuple “(50, 60)” as one of its items.

The above-given example can be written in another way as, let "b = (50, 60)" is a tuple and "a = (10, 20, 30, b)" is another tuple. Here, we can say that "a" is a nested tuple as it contains tuple “b” nesting inside it. Now, let us see how a tuple looks with two more tuples inside it. "a = ((10, 20, 30), (40, 50, 60))" is a nested tuple with indexing [0] and [1].

Here, we can see the indexing given to both the tuples as items of the main tuple. The items of the sub-tuples are also given indexing starting from the index [0]. This is what a nested tuple looks like in the python language.

Accessing a Nested Tuple

A nested tuple can be accessed similarly to how we access a general tuple stored with its elements. An extra pair of square brackets are used for every increase in dimension of the tuple. Suppose for the tuple “a = (10, 20, 30, (50, 60))”, if we want to access the element “10”, we use the statement “a [0]”. To access the two-dimensional element “50” from the tuple, we use the statement “a [3] [0]”.

Program for accessing a Nested Tuple

Now let us see a program for accessing an element in a nested tuple.

a = (10, 20, 30, (50, 60))
print (a [3] [0])

Output

Nested Tuple in Python

Related Topics

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Python Coroutine

Introduction In Python, Coroutines are defined as the special type of function that freely allows control to its caller without losing the state. Coroutines and generates are similar but coroutines consist...

3 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 minutes read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.