0

I would like to call multiple dictionaries using a for loop. I am unsure how to call multiple dictionaries properly, and I can't figure out if its possible using concatenation. The code below should explain my thinking even though the print statement is incorrect.

stat0 = {}
stat0["bob"] = 0
stat1 = {}
stat1["bob"] = 0
stat2 = {}
stat2["bob"] = 0

for i in range(3):
    print(stat(i))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Louis
  • 23
  • 2

2 Answers2

2

How about putting them in a collection, and then getting value generically:

for hm in [stat0, stat1, stat2]
    print(hm["bob"])
pellucidcoder
  • 127
  • 3
  • 9
1

Instead of naming your dictionaries, just put them into another dictionary:

#UNTESTED
stat = { 0: stat0, 1: stat1, 2: stat2 }
for i in range(3):
    print(stat[i])

Or, use an iterative style more appropriate for dict:

#UNTESTED
for i, d in stat.items():
    print(i, d)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308