Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create dictionary from predefined keys and values as the list?

Let’s say that I have 3 users and keys will be: ["a", "b", "c", "d"]. I would like to create a dictionary with the list of zeroes but the number of the zeroes in each key should match number of users, so I want to have it like that:
{"a": [0,0,0], "b": [0,0,0], "c": [0,0,0], "d", [0,0,0]}

What is the easiest way of doing this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

users=['u','v','w']
d={k:len(users)*[0] for k in ['a','b','c','d']}
print(d)

gives

{'a': [0, 0, 0], 'b': [0, 0, 0], 'c': [0, 0, 0], 'd': [0, 0, 0]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading