Possible Duplicate:
What's the best way to search for a Python dictionary value in a list of dictionaries?
I have a list of dictionaries in the form
my_dict_list = []
my_dict_list.append({'text':'first value', 'value':'number 1'})
my_dict_list.append({'text':'second value', 'value':'number 2'})
my_dict_list.append({'text':'third value', 'value':'number 3'})
I also have another list in the form:
results = ['number 1', 'number 4']
how can I loop through the list of results
checking if the value is in dict
, e.g.
for r in results:
if r in my_dict_list:
print "ok"
Using unique everseen() for Removing duplicate dictionaries in a list. everseen() function is used to find all the unique elements present in the iterable and preserving their order of occurrence. Hence it remembers all elements ever seen in the iterable.
Dictionaries do not support duplicate keys. However, more than one value can correspond to a single key using a list. For example, with the dictionary {"a": [1, 2]} , 1 and 2 are both connected to the key "a" and can be accessed individually.
Python list can contain duplicate elements.
for r in result:
for d in dict:
if d['value'] == r:
print "ok"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With