Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples.
Examples:
Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'),
(12.121, 'Tuple3'), (923232.2323, 'Tuple4')]
Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]
Input: [('Tuple1', 121), ('Tuple2', 125),
('Tuple1', 135), ('Tuple4', 478)]
Output: [('Tuple1', 121), ('Tuple2', 125), ('Tuple4', 478)]
Below are some ways to achieve the above task.
Method #1: Using Iteration
# Python code to remove tuples having # duplicate first value from given # list of tuples # Input list initialization Input = [(12.121, 'Geeksforgeeks is best'), (19212.22, 'India is best'), (12.121, 'Cyware is best.'), (923232.2323, 'Jiit is best')] # using set visited = set() # Output list initialization Output = [] # Iteration for a, b in Input: if not a in visited: visited.add(a) Output.append((a, b)) # Printing print("Initial list of tuple is \n", Input) print("List of tuple after removing duplicates:\n ", Output) |
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (12.121, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]List of tuple after removing duplicates:
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Jiit is best’)]
Method #2: Using list comprehension
# Python code to remove tuples having # duplicate first value from given # list of tuples # Input list initialization Input = [(12.121, 'Geeksforgeeks is best'), (19212.22, 'India is best'), (19212.22, 'Cyware is best.'), (923232.2323, 'Jiit is best')] # Using set seen = set() # using list comprehension Output = [(a, b) for a, b in Input if not (a in seen or seen.add(a))] # Printing print("Initial list of tuple is" \n, Input) print("\nList of tuple after removing duplicates \n", Output) |
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]List of tuple after removing duplicates [(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Jiit is best’)]
Method #3: Using itertools
# Python code to remove tuples having # duplicate first value from given # list of tuples import itertools # Input list initialization Input = [(12.121, 'Geeksforgeeks is best'), (19212.22, 'India is best'), (923232.2323, 'Cyware is best.'), (923232.2323, 'Jiit is best')] # Using groupby Output = ([next(b) for a, b in itertools.groupby( Input, lambda y: y[0])]) # Printing print("Initial list of tuple is\n", Input) print("\nList of tuple after removing duplicates\n", Output) |
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]List of tuple after removing duplicates
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Cyware is best.’)]
Method #4: Using OrderedDict
This is the most elegant way to remove duplicates is using OrderedDict.
# Python code to remove tuples having # duplicate first value from given # list of tuples from collections import OrderedDict # Input list initialization Input = [(12.121, 'Geeksforgeeks is best'), (19212.22, 'India is best'), (19212.22, 'Cyware is best.'), (923232.2323, 'Jiit is best')] # Using orderedDIct Output = OrderedDict(Input).items() # Printing print("Initial list of tuple is\n", Input) print("\nList of tuple after removing duplicates\n", Output) |
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]List of tuple after removing duplicates
odict_items([(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)])
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove duplicate lists in tuples (Preserving Order)
- Python - Remove Duplicate subset Tuples
- Python - Remove Tuples from the List having every element as None
- Python | Get sum of tuples having same first value
- Python | Find the tuples containing the given element from a list of tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Get duplicate tuples from list
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Get first element with maximum value in list of tuples
- Python | Group tuples in list with same first value
- Python - Convert Tuple value list to List of tuples
- Python | Remove unordered duplicate elements from a list
- Python - Remove duplicate words from Strings in List
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.

