Given a list:
source = [{'A':123},{'B':234},{'C':345}]
I need a dict from this list in the format:
newDict = {'A':123,'B':234,'C':345}
What is the syntactically cleanest way to accomplish this?
Use a dict-comprehension:
>>> l = [{'A':123},{'B':234},{'C':345}]
>>> d = {k: v for dct in l for k, v in dct.items()}
>>> d
{'A': 123, 'B': 234, 'C': 345}
However it's probably opinion-based if that's the "syntactically cleanest way" but I like it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With