Set Of Dictionaries In List Of Dictionarys
trying to find a set of dictionaries in a list. Say I have the following list of dictionaries: rm_dict = [{'name':'rick','subject':'adventure time mortttty buugh','body':['wubba lu
Solution 1:
Your error message is telling you something important: neither a dictionary or a list is hashable, and so cannot be used as a member of set. One way to work around that is to use the str
that is the 0th element of the email body in your data.
You can "uniqify" your list based on one of its keys with a list comprehension:
>>> seen = set()
>>> [i for i in rm_dict if i['body'][0] notin seen andnot seen.add(i['body'][0])]
[{'name': 'rick',
'subject': 'adventure time mortttty buugh',
'body': ['wubba lubba dub dubbb motha f*&^%!']},
{'name': 'morty',
'subject': 're:adventure time mortttty buugh',
'body': ['youre drunk rick!']}]
Here's another form, without the comprehension:
>>> seen = set()
>>> emails = []
>>> for i in rm_dict:
... body = i['body'][0]
... if body notin seen:
... emails.append(i)
... seen.add(body)
... >>> emails
[{'name': 'rick',
'subject': 'adventure time mortttty buugh',
'body': ['wubba lubba dub dubbb motha f*&^%!']},
{'name': 'morty',
'subject': 're:adventure time mortttty buugh',
'body': ['youre drunk rick!']}]
Solution 2:
Set items have to be hashable, which dicts are not. You can use pickle
to serialize all the dicts, then use set
to obtain unique items, and finally deserialize them back to dicts:
import pickle
print(list(map(pickle.loads, set(map(pickle.dumps, rm_dict)))))
This outputs:
[{'name': 'morty', 'subject': 're:adventure time mortttty buugh', 'body': ['youre drunk rick!']}, {'name': 'rick', 'subject': 'adventure time mortttty buugh', 'body': ['wubba lubba dub dubbb motha f*&^%!']}]
Post a Comment for "Set Of Dictionaries In List Of Dictionarys"