×

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 get current date in python?

How to get current date in python? In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate...

3 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Python memoryview() Function

Python memoryview() Function The memoryview() function in Python returns a memory view object from a specified object. Syntax memoryview(obj) Parameter obj: This parameter represents a Bytes object or a bytearray object. Return This function returns a “memory view” object...

1 minute read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Python Sys Stdout

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.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Tensorflow Angular in Python

TensorFlow is an open-source, Python-compatible toolkit for numerical computation that accelerates and simplifies the creation of neural networks and machine learning algorithms. They make the interesting notion that models can be...

6 minutes read.

How to convert integer to float in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

5 minutes read.

List Iteration in Python

In this tutorial, we will learn how to iterate list in Python. List in Python A list is an ordered group of values which includes several kinds of values.A list is a mutable...

3 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.