0

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

1 Answer 1

1

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)]
Sign up to request clarification or add additional context in comments.

1 Comment

It can be a one liner using dict-comprehension: {k: ','.join[f"{k}{j}" for j in range(1,11)] for k in keys}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.