Friday, 15 August 2014

python - Find value of a key within a list of dictionary given the key -



python - Find value of a key within a list of dictionary given the key -

i trying ['a','b'] next variables in python:

mylist = [{'a': ['a','b']}, {'b': ['c','d']}]

and

keys = ['a']

i have seen various questions answers using itemgetter work when keys of dictionaries within list same. (if utilize itemgetter on data, homecoming me keyerror)

does know how create work?

thanks!

i'd turn mylist single dictionary:

in [27]: d = dict(reduce(operator.add, (x.items() x in mylist)))

and query so:

in [28]: [d[k] k in keys] out[28]: [['a', 'b']]

python list dictionary

No comments:

Post a Comment