Long Vuong
Long Vuong

Reputation: 181

How do I create a list with the number I want in a loop?

Say I want to create a new list every (in a loop) time the variable i (an integer) changes, eg:

game_1 = []

game_2 = []

game_3 = []

...

game_i = []

How would I do this?

I've tried putting game_i, which obviously doesn't work; neither does game[i],

Upvotes: 1

Views: 63

Answers (2)

Pratik Patel
Pratik Patel

Reputation: 353

gb = globals()
for i in range(10):
    gb['game_{0}'.format(i)] = []

Upvotes: 2

Wazaki
Wazaki

Reputation: 899

I guess you are trying to dynamically name variables. This issue has been discussed before, check This link for details of alternatives.

Edit: second link you should consider Link

Upvotes: 3

Related Questions