Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - value in list of dictionaries [duplicate]

Tags:

python

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"
like image 990
John Avatar asked Apr 14 '11 14:04

John


People also ask

How do I remove duplicates from a dictionary list?

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.

Can dictionaries have duplicate values Python?

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.

Can Python lists have duplicate values?

Python list can contain duplicate elements.


1 Answers

for r in result:
  for d in dict:
    if d['value'] == r:
       print "ok"
like image 84
Troydm Avatar answered Oct 21 '22 06:10

Troydm