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

Python creating dictionaries in loop

Can anyone please help me in creating dictionary in loop.

I want my dictionary to be

Input
d={'a':'a1,a2,a3,.....,a10', 'b':'b1,b2,b3,....,b10'}

print(d) 
{'a': 'a1,a2,a3,a4,a5,a6,a7,a8,a9,a10', 'b': 'b1,b2,b3,b4,b5,b6,b7,b8,b9,b10'}

Reason for asking this I have huge amount of dictionaries to make. Trying to make it a smart way

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 :

Is this what you’re looking for?

d = dict()
keys = ['a','b']
for i in keys:
  d[i] = [f"{i}{j}" for j in range(1,11)]

Note that if you want a single string, just write this inside the loop:

d[i] = ','.join[f"{i}{j}" for j in range(1,11)]
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