Nested Tuples in Python29 Aug 2024 | 4 min read 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 TupleCode Output: ((10, 'Itika', 13000),) ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) Some Operations of Nested TuplesWe will see two necessary operations of nested tuples. Concatenating Tuples to Nested TuplesWhen 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 "," OperatorThis 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 TuplesWe 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)] |
What is Sorting? Sorting is the technique used to arrange the data in a specific order. It arranges the numerical data in ascending or descending order. It is used to represent the data in a simpler way, which is much more understandable. Various algorithms are made based...
6 min read
In this tutorial, we will write the program to create desktop/laptop battery notifier and send the battery percentage notification using Python programming language. As a laptop user, we must aware of the battery percentage of the laptop. What if we get a program that reminds us...
5 min read
Sentiment analysis is a process that 'computationally determines' whether a piece is positive, neutral, or negative. It is also known as opinions mining. This process determines whether a sentence of the paragraph is negative, positive, or neutral. Business: Marketing companies use it to create strategies, understand customers'...
3 min read
In Python, we know how the arithmetic operators can be used to add, subtract, divide and multiply two variables. In this article, we will learn how we can extend the functionality of an operator in a precise form at the time of evaluating an expression. Let us have...
3 min read
Python is one of the most popular programming languages, known for its simplicity and readability. However, as a language, it is still susceptible to bugs and errors, which can cause significant issues for developers. To mitigate these problems, the Python community has developed a number of...
6 min read
You might need to locate the first item in a Python iterable, like a list or dictionary, that meets a specific requirement at an indeterminate point in your Python trip. The sole exception is when it is necessary to confirm "that a" specific item is present in...
13 min read
In this tutorial, we will learn how to calculate the sum of squares of first n natural numbers using Python. We have a positive integer "N", and our task is to calculate (12 + 22 + 32 + 42 + 52 +… + N2) Example: Input: N =...
3 min read
The name of the Python script or module currently executing is stored in Python's __name__ special variable. Python 3.0 introduced the __name__ variable, which is absent from Python 2. x. The current Python script or module's value, __main__, is assigned to the __name__ variable while it...
5 min read
In definition, private variables would be those that can only be seen and accessed by members of the class to which they belong, not by members of any other class. When the programme runs, these variables are utilised to access the values to keep the information...
3 min read
In Mathematics, we use null to represent nothing, and in some programming languages such as C and Java, NULL represents the same, but in Python, it is different. Generally, in other programming languages, null is used when a pointer point to nothing or when a variable...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India