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

>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)]

Leave a Reply