Echchama Nayak
Echchama Nayak

Reputation: 933

Filter list of dictionaries with duplicate values at a certain key

I have a dictionary as follows:

dic=[{'a':1,'b':2,'c':3},{'a':9,'b':2,'c':2},{'a':5,'b':1,'c':2}]

I would like to filter out those dictionaries with recurring values for certain keys, such as in this case the key 'b' which has duplicate values in the first and second dictionaries in the list. I would like to remove the second entry

Quite simply, I would like my filtered list to look as follows:

filt_dic=[{'a':1,'b':2,'c':3},{'a':5,'b':1,'c':2}]

Is there a pythonic way to do this?

Upvotes: 2

Views: 1976

Answers (1)

tobias_k
tobias_k

Reputation: 82929

Use another dictionary (or defaultdict) to keep track of what values you have already seen for what keys. This dictionary will hold one set (for fast lookup) for each key of the original dict.

dic=[{'a':1,'b':2,'c':3},{'a':9,'b':2,'c':2},{'a':5,'b':1,'c':2}]

seen = defaultdict(set)
filt_dic = []
for d in dic:
    if not any(d[k] in seen[k] for k in d):
        filt_dic.append(d)
        for k in d:
            seen[k].add(d[k])

print(filt_dic)

Afterwards, filt_dic is [{'a': 1, 'c': 3, 'b': 2}, {'a': 5, 'c': 2, 'b': 1}] and seen is {'a': set([1, 5]), 'c': set([2, 3]), 'b': set([1, 2])}).

Upvotes: 2

Related Questions