Python | Remove tuples having duplicate first value from given list of tuples
Last Updated :
28 Mar, 2023
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
Follow the below steps to implement the above idea:
- First, a list of tuples named Input is initialized with four tuples. Each tuple has two elements: a floating-point number and a string.
- An empty set named visited is created to store the unique first values of the tuples.
- An empty list named Output is created to store the tuples with unique first values.
- The program iterates over each tuple in the Input list using a for loop with two variables, a and b, to represent the first and second elements of each tuple, respectively.
- Inside the loop, the program checks if the first value a is already in the set visited using the in keyword. If it is not in the set, the program adds it to the set using the add() method and appends the entire tuple (a, b) to the Output list using the append() method.
- After all the tuples have been checked, the program prints the original list of tuples Input using the print() function with a string message. Then it prints the list of tuples with unique first values Output using the print() function with another string message.
Below is the implementation of the above approach:
Python3
Input = [( 12.121 , 'Geeksforgeeks is best' ),
( 19212.22 , 'India is best' ),
( 12.121 , 'Cyware is best.' ),
( 923232.2323 , 'Jiit is best' )]
visited = set ()
Output = []
for a, b in Input :
if not a in visited:
visited.add(a)
Output.append((a, b))
print ( "Initial list of tuple is \n" , Input )
print ( "List of tuple after removing duplicates:\n " , Output)
|
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’)]
Time Complexity: O(n), where n is the number of tuples in the Input list.
Auxiliary Space: O(n), as we are using a set to keep track of visited first values and a list to store the output tuples.
Method #2: Using list comprehension
Approach:
- Initialize a list of tuples named “Input” with some values.
- Create an empty set named “seen”.
- Use list comprehension to iterate over the tuples in the “Input” list and extract the first and second element of each tuple into variables “a” and “b”, respectively.
- Check if “a” is already in the “seen” set or not, and add it to the set if it is not already there. If “a” is already in the “seen” set, do not add the tuple to the output list.
- If the tuple passes the above condition, add it to a new list named “Output”.
- Print the original input list of tuples.
- Print the new list of tuples obtained after removing tuples with duplicate first elements.
Python3
Input = [( 12.121 , 'Geeksforgeeks is best' ),
( 19212.22 , 'India is best' ),
( 19212.22 , 'Cyware is best.' ),
( 923232.2323 , 'Jiit is best' )]
seen = set ()
Output = [(a, b) for a, b in Input
if not (a in seen or seen.add(a))]
print ( "Initial list of tuple is" \n, Input )
print ( "\nList of tuple after removing duplicates \n" , Output)
|
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’)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using itertools
Python3
import itertools
Input = [( 12.121 , 'Geeksforgeeks is best' ),
( 19212.22 , 'India is best' ),
( 923232.2323 , 'Cyware is best.' ),
( 923232.2323 , 'Jiit is best' )]
Output = ([ next (b) for a, b in itertools.groupby(
Input , lambda y: y[ 0 ])])
print ( "Initial list of tuple is\n" , Input )
print ( "\nList of tuple after removing duplicates\n" , Output)
|
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.’)]
Time complexity: O(nlogn), where n is the length of the input list.
Auxiliary space: O(n), as the output list may have up to n elements (when there are no duplicates in the input list).
Method #4: Using OrderedDict This is the most elegant way to remove duplicates is using OrderedDict.
Python3
from collections import OrderedDict
Input = [( 12.121 , 'Geeksforgeeks is best' ),
( 19212.22 , 'India is best' ),
( 19212.22 , 'Cyware is best.' ),
( 923232.2323 , 'Jiit is best' )]
Output = OrderedDict( Input ).items()
print ( "Initial list of tuple is\n" , Input )
print ( "\nList of tuple after removing duplicates\n" , Output)
|
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’)])
Time complexity: O(n), where n is the number of tuples in the input list
Auxiliary space: O(n), where n is the number of tuples in the input list.
Method#5: Using Recursive method
Python3
def remove_duplicates(lst):
if len (lst) < = 1 :
return lst
else :
for i in range ( 1 , len (lst)):
if lst[ 0 ][ 0 ] = = lst[i][ 0 ]:
lst.pop(i)
return [lst[ 0 ]] + remove_duplicates(lst[ 1 :])
return [lst[ 0 ]] + remove_duplicates(lst[ 1 :])
Input = [( 12.121 , 'Geeksforgeeks is best' ),
( 19212.22 , 'India is best' ),
( 19212.22 , 'Cyware is best.' ),
( 923232.2323 , 'Jiit is best' )]
Output = remove_duplicates( Input )
print ( 'Initial list of tuple is\n' , Input )
print ( "List of tuple after removing duplicates \n" , Output)
|
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')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using dictionary
Use a dictionary to remove the tuples having a duplicate first value. In this approach, you can iterate through the list of tuples, and for each tuple, you can check if the first value is already present in the dictionary. If it is not present, you can add the key-value pair to the dictionary. If it is present, you can skip the tuple.
Python3
Input = [( 12.121 , 'Geeksforgeeks is best' ),
( 19212.22 , 'India is best' ),
( 12.121 , 'Cyware is best.' ),
( 923232.2323 , 'Jiit is best' )]
visited = {}
Output = []
for a, b in Input :
if a not in visited:
visited[a] = True
Output.append((a, b))
print ( "Initial list of tuple is\n" , Input )
print ( "List of tuple after removing duplicates:\n" , Output)
|
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')]
Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(k), where k is the number of unique first values in the input list.
Please Login to comment...