Is there a python way of initialization of a dictionary?
animals = ["dog","cat","cow"]
for x in animals:
primers_pos[x]={}
Is there something like
(primers_pos[x]={} for x in animals)
You can use a dictionary comprehension (supported in Python 2.7+):
>>> animals = ["dog", "cat", "cow"]
>>> {x: {} for x in animals}
{'dog': {}, 'cow': {}, 'cat': {}}
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