Open In App

Python Group by matching second tuple value in list of tuples

Improve
Improve
Like Article
Like
Save
Share
Report

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), (27, 11)],
         22: [(1, 22)]}

Input : [(20, 'Geek'), (31, 'Geek'), (88, 'NotGeek'), (27, 'NotGeek')]
Output: {'NotGeek': [(88, 'NotGeek'), (27, 'NotGeek')],
         'Geek': [(20, 'Geek'), (31, 'Geek')]}

  Code #1: 

Python3




# Python program to group tuples by matching
# second tuple value in list of tuples
 
# Initialisation
Input = [(20, 80), (31, 80), (1, 22), (88, 11), (27, 11)]
 
Output = {}
for x, y in Input:
    if y in Output:
        Output[y].append((x, y))
    else:
        Output[y] = [(x, y)]
 
# Printing Output
print(Output)


Output:

{80: [(20, 80), (31, 80)], 11: [(88, 11), (27, 11)], 22: [(1, 22)]}

Time Complexity: O(n)
Auxiliary Space: O(n)

  Code #2: 

Python3




# Python program to group tuples by matching
# second tuple value in list of tuples
 
# Initialisation
Input = [(20, 'Geek'), (31, 'Geek'), (88, 'NotGeek'), (27, 'NotGeek')]
 
Output = {}
for x, y in Input:
    if y in Output:
        Output[y].append((x, y))
    else:
        Output[y] = [(x, y)]
 
# Printing Output
print(Output)


Output:

{'NotGeek': [(88, 'NotGeek'), (27, 'NotGeek')],
 'Geek': [(20, 'Geek'), (31, 'Geek')]}

  Code #3: 

Python3




# Python program to group tuples by matching
# second tuple value in list of tuples
 
# Initialisation
def group_by_second(lst):
    return {k: [(x, y) for x, y in lst if y == k] for k in {y for x, y in lst}}
 
# Example usage
lst=[(20, 'Geek'), (31, 'Geek'), (88, 'NotGeek'), (27, 'NotGeek')]
# Printing Output
 
print(group_by_second(lst))
#This code is contributed by Edula Vinay Kumar Reddy


Output

{'Geek': [(20, 'Geek'), (31, 'Geek')], 'NotGeek': [(88, 'NotGeek'), (27, 'NotGeek')]}

In this version of the function, the dictionary comprehension iterates over the set of unique second elements {y for x, y in lst} and creates a key-value pair for each element, with the value being a list of tuples with a matching second element. The list comprehension [(x, y) for x, y in lst if y == k] filters the tuples in the input list by their second element and creates a new tuple for each element that matches the current second element k.

For example, given the input list [(20, 80), (31, 80), (1, 22), (88, 11), (27, 11)], the function will create a dictionary with three keys: 80, 11, and 22. The value for the key 80 will be the list [(20, 80), (31, 80)], the value for the key 11 will be the list [(88, 11), (27, 11)], and the value for the key 22 will be the list [(1, 22)].

The time complexity of the group_by_second function is O(n), where n is the length of the input list. This is because the function iterates over the input list once to create the dictionary.

The space complexity of the function is also O(n), because the function creates a new list and a new dictionary with n elements.



Last Updated : 16 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads