python - Finding the dictionary a key value comes from -


lets have list of dictionaries, ranging in thousands. every dictionary has exact same keys, different values.

is there way lookup dictionary key value comes from, , access different key values same dictionary?

for example, have list containing every person in large city, dictionary format, so:

people_in_city = [          {'name': 'bob jang',         'age': 45,         'sex': 'male'},         {'name': 'barb esau',         'age': 56,         'sex': 'female'},         etc.         etc. 

now, want lookup age of bob jang, have name. there anyway corresponding age using format?

there's no fast way in python. i'd suggest like:

def get_dict_from_key(list_of_dicts, key, value):     return next((d d in list_of_dicts if d[key] == value))  result_d = get_dict_from_key(dicts, 'name', 'bob jang') result_d['age'] 

that said, kind of thing relational databases made for! learn sql , use :)


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -