list = [{ key1:value1, key2:value2, key3:value3,........}, { key:value, key2:value2,...}, { key:value, key2:value2,...}]
The elements of the list are in the form of Dickshire How can I find the value using key1 in the list above?
python list dictionary
>>> from collections import ChainMap
>>> L = [{'key1': 111, 'key2': 222}, {'key3': 333, 'key4': 444}]
>>> cm = ChainMap(*L)
>>> cm['key1']
111
Of course, you can merge with each other with updates from dict.
Alternatively, the newly introduced grammar from 3.5 can be combined as follows.
>>> L = [{'key1': 111, 'key2': 222}, {'key3': 333, 'key4': 444}]
>>> {**L[0], **L[1]}
{'key1': 111, 'key2': 222, 'key3': 333, 'key4': 444}
© 2024 OneMinuteCode. All rights reserved.