Python | Remove tuples having duplicate first value from given list of tuples - GeeksforGeeks

29 Mar, 2019
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 initializationInput = [(12.121, 'Geeksforgeeks is best'),          (19212.22, 'India is best'),          (12.121, 'Cyware is best.'),         (923232.2323, 'Jiit is best')]  # using setvisited = set()  # Output list initializationOutput = []  # Iterationfor a, b in Input:    if not a in visited:        visited.add(a)        Output.append((a, b))  # Printingprint("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 initializationInput = [(12.121, 'Geeksforgeeks is best'),          (19212.22, 'India is best'),          (19212.22, 'Cyware is best.'),          (923232.2323, 'Jiit is best')]  # Using setseen = set()  # using list comprehensionOutput = [(a, b) for a, b in Input          if not (a in seen or seen.add(a))]  # Printingprint("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 initializationInput = [(12.121, 'Geeksforgeeks is best'),          (19212.22, 'India is best'),          (923232.2323, 'Cyware is best.'),          (923232.2323, 'Jiit is best')]  # Using groupbyOutput = ([next(b) for a, b in itertools.groupby(                         Input, lambda y: y[0])])  # Printingprint("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 initializationInput = [(12.121, 'Geeksforgeeks is best'),          (19212.22, 'India is best'),          (19212.22, 'Cyware is best.'),          (923232.2323, 'Jiit is best')]  # Using orderedDIctOutput = OrderedDict(Input).items()  # Printingprint("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 duplicatesodict_items([(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)])
Python tuple-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary