- 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
Remove duplicate lists in tuples (Preserving Order) in Python
When it is required to remove the duplicates present in tuple of list, as well as preserving the order, a list comprehension and the 'set' method can be used.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
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_tuple_1 = ([1, 21, 34] , [11, 0, 98], [45, 67, 56]) print("The tuple of list is : ") print(my_tuple_1) temp_val = set() my_result = [elem for elem in my_tuple_1 if not(tuple(elem) in temp_val or temp_val.add(tuple(elem)))] print("The unique tuple of list is : ") print(my_result)
The tuple of list is : ([1, 21, 34], [11, 0, 98], [45, 67, 56]) The unique tuple of list is : [[1, 21, 34], [11, 0, 98], [45, 67, 56]]
- A tuple of list is defined, and is displayed on the console.
- An empty set is created.
- The tuple of list is iterated over, and if it is not present in the previously defined list, it is added to the list.
- This would result in a set that contains unique values.
- This is assigned to a value.
- It is displayed on the console.
- Remove duplicate tuples from list of tuples in Python
- Remove tuples having duplicate first value from given list of tuples in Python
- Remove matching tuples in Python
- Remove Tuples of Length K in Python
- Remove tuples from list of tuples if greater than n in Python
- What's the difference between lists and tuples in Python?
- Python | Remove empty tuples from a list
- Python program to Order Tuples using external List
- What are the differences and similarities between tuples and lists in Python?
- Sort Tuples in Increasing Order by any key in Python program
- Remove Duplicate Letters in C++
- Python – Remove Columns of Duplicate Elements
- Python program to sort tuples in increasing order by any key.
- Program to remove duplicate entries in a list in Python
- Python – Remove Tuples with difference greater than K