Python | Get unique tuples from list
Sometimes, while working with Python list, we can come across a problem in which we require to find the unique occurrences of list. Having elementary data types is easy to handle, but sometime, we might have complex data types and the problem becomes new in that cases. Let’s discuss certain ways in which tuples are handled for this problem.
Method #1 : Using list() + set()
Alike integers, being immutable these can also be handled by set() for removal of the duplicates. It converts the list to set and removes duplicates and converted back to list by list()
# Python3 code to demonstrate working of # Get unique tuples from list # using set() + list() # initializing list test_list = [(4, 5), (6, 1), (4, 5), (6, 1)] # printing original list print("The original list is : " + str(test_list)) # Get unique tuples from list # using set() + list() res = list(set(test_list)) # printing result print("List after removal of duplicates " + str(res)) |
The original list is : [(4, 5), (6, 1), (4, 5), (6, 1)] List after removal of duplicates [(4, 5), (6, 1)]
Method #2 : Using dict.fromkeys() + list()
Since newer versions of Python dictionaries, remember their order of insertion, the list contents can be converted to dictionary element list, which remembers the order and removes the duplicates. It is converted back using list().
# Python3 code to demonstrate working of # Get unique tuples from list # using dict.fromkeys() + list() # initializing list test_list = [(4, 5), (6, 1), (4, 5), (6, 1)] # printing original list print("The original list is : " + str(test_list)) # Get unique tuples from list # using dict.fromkeys() + list() res = list(dict.fromkeys(test_list)) # printing result print("List after removal of duplicates " + str(res)) |
The original list is : [(4, 5), (6, 1), (4, 5), (6, 1)] List after removal of duplicates [(4, 5), (6, 1)]
Recommended Posts:
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Convert string tuples to list tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Combining tuples in list of tuples
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert list of tuples to list of list
- Python | Convert list of tuples into list
- Python | Update a list of tuples using another list
- Python | Min and Max value in list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



