Python - Find the desired value in the element in the dictionary form in the list

Asked 2 years ago, Updated 2 years ago, 109 views

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

2022-09-21 18:59

1 Answers

>>> 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}


2022-09-21 18:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.