-2

The variables have already been declared in advance.
They are to be used for various functions such as append().

The idea:
I want to use several variables (var1, var2, ..., var90) in a for loop.
So that I do not have to call each variable individually, I want to solve the whole simplified.

The problem:
What is my approach to this?
My current attempt was with exec, but I hardly know anything about it.


Here is my updated code:

for el in range(1, 91):
  exec(f'var{el} = []')
  exec(f'var{el}.append("Some Value")')
JueK3y
  • 267
  • 9
  • 22
  • 1
    What is the problem you are trying to solve? – Srinivas Reddy Thatiparthy Mar 08 '22 at 10:57
  • 2
    This is much better suited for a `dict`, or even a `list` if you only need indexes up to 90. – Peter Mar 08 '22 at 10:57
  • 3
    Does this answer your question? [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – Maurice Meyer Mar 08 '22 at 10:58
  • @MauriceMeyer It doesn't. I don't want to create variables, i want to re-fill them in a loop – JueK3y Mar 08 '22 at 11:49
  • @SrinivasReddyThatiparthy How can I call several variables, which differ only by a number in the name, in a for loop without calling them individually each time? – JueK3y Mar 08 '22 at 11:56
  • @JueK3y You said that those variables already exist and you want to bind them to new values. Well in that case you have to start earlier. Rewrite your program to not use those variables and use a dictionary or a list instead. There is no sane reason to create a hackish and unmaintainable code with variable variable names when Python already gives you the data structures for that task. – Matthias Mar 08 '22 at 12:25

1 Answers1

2

Typically, exec is frowned upon (but I am aware that it might have its uses!).

There are several ways to approach this. Either use lists or dicts. They are designed to have some looping in them, or even have special things designed for them (such as list comprehensions).

In your example you already have created some kind of list (range(1,91), although technically that's a generator). If you were to create an actual list, you can use that. For example:

a_list = []
for i in range(90):
    a_list.append('') # to create a list of empty strings

In that case, you can still use the variable a_list containing all your values. And in list comprehension you can do the fancy stuff.

PrinsEdje80
  • 494
  • 4
  • 8
  • Thanks for the answer! The variables I created are lists themselves. Is it possible to continue using the individual variables as lists with your method? – JueK3y Mar 09 '22 at 06:40
  • 1
    The great thing about Python is that you can nest lists, i.e. [lists inside](https://www.delftstack.com/howto/python/list-of-lists-in-python/) lists (and list inside lists inside lists inside lists... ad infinitum). So, it's possible to do `for i in range(90): for j in range(100): print(a_list[i][j])`. With respect to list comprehension, you can have [nested list comprehension](https://www.geeksforgeeks.org/nested-list-comprehensions-in-python/). – PrinsEdje80 Mar 09 '22 at 10:25