×

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

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 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.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 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.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Python Scikit-image | Image Processing Using Scikit-Image

What is Image Processing? The world is defined with images and, every image has its different specialties. An image can contain much-needed information that can be helpful in various ways. The process...

4 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.

Python map() function

Python map() function The map() function in Python returns an iterator that applies a function to every item of iterable, yielding the results. Syntax map(function, iterable, ...) Parameter function: It is a required parameter that represents the function to...

1 minute read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.