Python – Pairs with multiple similar values in dictionary

Sometimes, whilw working with dictionaries, we can have a problem in which we need to keep the dictionaries which are having similar key’s value in other dictionary. This is a very specific problem. This can have applications in web development domain. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension
This task can be performed using list comprehension. In this we iterate each element in list and nesting loop is run to match key’s value with every other value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Pairs with multiple similar values in dictionary
# using list comprehension
  
# Initializing list
test_list = [{'Gfg' : 1, 'is' : 2}, {'Gfg' : 2, 'is' : 2}, {'Gfg' : 1, 'is' : 2}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Pairs with multiple similar values in dictionary
# using list comprehension
res = [sub for sub in test_list if len([ele for ele in test_list if ele['Gfg'] == sub['Gfg']]) > 1]
  
# printing result 
print ("List after keeping dictionary with same key's value : " + str(res))

chevron_right


Output :

The original list is : [{'is': 2, 'Gfg': 1}, {'is': 2, 'Gfg': 2}, {'is': 2, 'Gfg': 1}]
List after keeping dictionary with same key's value : [{'is': 2, 'Gfg': 1}, {'is': 2, 'Gfg': 1}]

 

Method #2 : Using Counter() + list comprehension
In this method, the task of finding the frequency is performed using Counter() and then task of finding the elements having multiple keys value is done using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Pairs with multiple similar values in dictionary
# using list comprehension + Counter()
from collections import Counter
  
# Initializing list
test_list = [{'Gfg' : 1, 'is' : 2}, {'Gfg' : 2, 'is' : 2}, {'Gfg' : 1, 'is' : 2}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Pairs with multiple similar values in dictionary
# using list comprehension + Counter()
temp = Counter(sub['Gfg'] for sub in test_list)
res = [ele for ele in test_list if temp[ele['Gfg']] > 1]
  
# printing result 
print ("List after keeping dictionary with same key's value : " + str(res))

chevron_right


Output :

The original list is : [{'is': 2, 'Gfg': 1}, {'is': 2, 'Gfg': 2}, {'is': 2, 'Gfg': 1}]
List after keeping dictionary with same key's value : [{'is': 2, 'Gfg': 1}, {'is': 2, 'Gfg': 1}]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.