- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get unique elements in nested tuple in Python
When it is required to get the unique elements in a nested tuple, a nested loop and the 'set' operator can be used.
Python comes with a datatype known as 'set'. This 'set' contains elements that are unique only.
The set is useful in performing operations such as intersection, difference, union and symmetric difference.
Below is a demonstration of the same −
my_list_1 = [(7, 8, 0), (0 ,3, 45), (3, 2, 22), (45, 12, 9)] print ("The list of tuple is : " ) print(my_list_1) my_result =  temp = set() for inner in my_list_1: for elem in inner: if not elem in temp: temp.add(elem) my_result.append(elem) print("The unique elements in the list of tuples are : ") print(my_result)
The list of tuple is : [(7, 8, 0), (0, 3, 45), (3, 2, 22), (45, 12, 9)] The unique elements in the list of tuples are : [7, 8, 0, 3, 45, 2, 22, 12, 9]
- A list of tuple is defined, and is displayed on the console.
- An empty list is created, and an empty set is created.
- The list is iterated through, and checked to see if it is present in the list.
- If not, it is added to the list as well as the empty set.
- This result is assigned to a value.
- It is displayed as output on the console.
- How to append elements in Python tuple?
- Cummulative Nested Tuple Column Product in Python
- Remove nested records from tuple in Python
- Convert Nested Tuple to Custom Key Dictionary in Python
- How to count elements in a nested Python dictionary?
- Delete Tuple Elements in Python
- How to get the remaining elements of the Tuple in C#?
- Python program to Flatten Nested List to Tuple List
- Modulo of tuple elements in Python
- Raise elements of tuple as power to another tuple in Python
- Python - Unique keys count for Value in Tuple List
- Python program to unique keys count for Value in Tuple List
- Python - Join tuple elements in a list
- Program to find sum of unique elements in Python
- Get tuple element data types in Python