How to filter out dictionaries with the same value for a certain key from a list?
Question:
I have the following list of objects:
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
I want to filter it based on the name, and because "Mike" is a duplicate in this case, I want to remove all entries with name=Mike
(regardless of the age).
(i.e. to get the same list but without the entries that have name=Mike
)
What’s the best approach to do that?
Answers:
You can use a Counter
to get the counts of all names. Then filter the names that appear more than once:
from collections import Counter
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
names_count = Counter(d['name'] for d in l)
new_l = list(filter(lambda d: names_count[d['name']] == 1, l))
print(new_l)
Will give:
[{'name': 'Peter', 'age': 29}]
Collecting the dicts in a dict where keys are names and values are either the only dict with that name or None
if the name is duplicated.
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
D = {}
for d in l:
n = d['name']
D[n] = None if n in D else d
l[:] = filter(None, D.values())
print(l)
And if you want to implement it without using any function.
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
name_dict = {}
for i in l:
if i['name'] not in name_dict:
name_dict[i['name']] = 1
else:
name_dict[i['name']] += 1
new_l = [d for d in l if name_dict[d['name']] == 1]
print(new_l)
outputs:
[{'name': 'Peter', 'age': 29}]
I have the following list of objects:
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
I want to filter it based on the name, and because "Mike" is a duplicate in this case, I want to remove all entries with name=Mike
(regardless of the age).
(i.e. to get the same list but without the entries that have name=Mike
)
What’s the best approach to do that?
You can use a Counter
to get the counts of all names. Then filter the names that appear more than once:
from collections import Counter
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
names_count = Counter(d['name'] for d in l)
new_l = list(filter(lambda d: names_count[d['name']] == 1, l))
print(new_l)
Will give:
[{'name': 'Peter', 'age': 29}]
Collecting the dicts in a dict where keys are names and values are either the only dict with that name or None
if the name is duplicated.
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
D = {}
for d in l:
n = d['name']
D[n] = None if n in D else d
l[:] = filter(None, D.values())
print(l)
And if you want to implement it without using any function.
l = [{'name': 'Mike', 'age': 31},
{'name': 'Peter', 'age': 29},
{'name': 'Mike', 'age': 44}]
name_dict = {}
for i in l:
if i['name'] not in name_dict:
name_dict[i['name']] = 1
else:
name_dict[i['name']] += 1
new_l = [d for d in l if name_dict[d['name']] == 1]
print(new_l)
outputs:
[{'name': 'Peter', 'age': 29}]