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))
DavidG
  • 21,958
  • 13
  • 81
  • 76
Louis
  • 23
  • 2
  • 1
    Did any answer answered you question? – Elis Byberi Oct 27 '17 at 13:58
  • Possible duplicate of [How to get the value of a variable given its name in a string?](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) – Elis Byberi Oct 27 '17 at 15:45

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
  • 131
  • 4
  • 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ᵩ
  • 154,489
  • 17
  • 222
  • 296