Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to efficiently remove elements from dicts that have certain value patterns?

For example, in dict1 the keys 1, 2, 3 all have the same value ‘a’, but the keys 3 and 5 have different values, ‘b’ and ‘d’. What I want is:

If N keys have the same value and N >=3, then I want to remove all other elements from the dict and only keep those N key values, which means ‘b’ & ‘d’ have to be removed from the dict.

The following code works, but it seems very verbose. Is there a better way to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from collections import defaultdict

dict1 = {1:'a', 2:'a', '3':'b', '4': 'a', '5':'d'}
l1 = [1, 2, 3, 4, 5]

dict2 = defaultdict(list)
for k, v in dict1.items():
    dict2[v].append(k)

to_be_removed = []
is_to_be_removed = False
for k, values in dict2.items():
    majority = len(values)
    if majority>=3:
        is_to_be_removed = True
    else:
        to_be_removed.extend(values)

if is_to_be_removed:
    for d in to_be_removed:
        del dict1[d]

print(f'New dict: {dict1}')

>Solution :

You can use collections.Counter to get the frequency of every value, then use a dictionary comprehension to retain only the keys that have the desired corresponding value:

from collections import Counter

dict1 = {1:'a', 2:'a', '3':'b', '4': 'a', '5':'d'}
ctr = Counter(dict1.values())

result = {key: value for key, value in dict1.items() if ctr[value] >= 3}
print(result)

This outputs:

{1: 'a', 2: 'a', '4': 'a'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading