0

As of right now i am a beginner with python. I wanna make a giant list of variables without having to write out each one, so far heres the idea:

    n=0
i=None
for i in range(10):
    n = n + 1
    x="hi"
    var = x+"{0}".format(n)
    print(var)

the output are strings but i want to make them variable that i can define freely. Any pointers>

  • 1
    Use a data structure here. No need to generate variables. – Christian Dean May 22 '18 at 18:58
  • 1
    Use a dictionary, it's almost always a terrible idea to have tonnes of generated names floating about in the namespace (I'm using "almost" to cover my back; I can't think of a good time to do it) – roganjosh May 22 '18 at 18:59
  • You can use list and you can access multiple values through same variable name using indices.In the example you are trying to get hi0,hi1,hi2 variables instead use hi as list and access by hi[0] hi[1] and so on – Ash Ishh May 22 '18 at 19:02

0 Answers0