May-02-2019, 05:19 AM
List of dictionaries makes usually sense when keys are the same. Then it's easy to manipulate data with comprehension.
This structure is prone to errors (what if new dictionaries will be added? new dictionaries with some keys overlapping with existing ones etc). I recommend to think about your needs and determine whether there can be better ways to store data.
It can be done manually, but this is not generalised solution, it works on this particular dataset and may break if changes are made (code assumes that any match will suffice, not all must match):
This structure is prone to errors (what if new dictionaries will be added? new dictionaries with some keys overlapping with existing ones etc). I recommend to think about your needs and determine whether there can be better ways to store data.
It can be done manually, but this is not generalised solution, it works on this particular dataset and may break if changes are made (code assumes that any match will suffice, not all must match):
>>> from collections import OrderedDict >>> lst = [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]), ... OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')])] >>> value_to_search = lst[0]['SecondName'] >>> if value_to_search in lst[1].values(): ... print(lst[0]['Numbers']) ... 15
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.