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.
Similar Reads
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Generating a "Set Of Tuples" from A "List of Tuples" - Python
We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will
3 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial lis
6 min read
Convert Tuple Value List to List of Tuples - Python
We are given a dictionary with values as a list of tuples and our task is to convert it into a list of tuples where each tuple consists of a key and its corresponding value. Note: Each key will appear with each value from the list of tuples. For example: We have a dictionary dict = {'Gfg' : [(5, ),
4 min read
Python - Remove Tuples from the List having every element as None
Given a Tuple list, remove all tuples with all None values. Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )] Output : [(None, 2), (3, 4), (12, 3)] Explanation : All None tuples are removed.Input : test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )] Output : [(
6 min read
Removing Tuples from a List by First Element Value - Python
In this problem we need to delete tuples based on a specific condition related to their first element. For example: We are given the list data = [("GeeksforGeeks", "Python", 1000), ("CodingForAll", "Java", 1200)] and we need to remove all tuples where the first element is "GeeksforGeeks", the desire
3 min read
Python | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th
8 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values. [GFGTABS] Python a = ["Learn
3 min read
Python Group by matching second tuple value in list of tuples
Given a list of tuples, the task is to group the tuples by matching the second element in the tuples. We can achieve this using dictionary by checking the second element in each tuple. Examples: Input : [(20, 80), (31, 80), (1, 22), (88, 11), (27, 11)] Output: {80: [(20, 80), (31, 80)], 11: [(88, 11
3 min read
Python - Remove duplicate words from Strings in List
Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The
6 min read