-1

Here is my problem I want to create variables in a loop and changing the suffix each loop I do.

For instance I want to create this, in a loop :

ax1=liste[1]
ax2=liste[2]
ax3=liste[3]

etc,etc

But I do not know how to change the number at the end without using str type. Someone have a clean way to do it? Thanks

EqElahin
  • 1
  • 1

3 Answers3

1

In general, dynamically naming variables should be avoided. That's what dictionaries are for. More on that here.

As for achieving this with a dictionary, in the context of your question it could look like:

ax = {}
for i in range(1,4):
    ax[i] = liste[i]

However, if you really must do it with separate variables, you could add it directly to the global variables, like so:

for i in [1,2,3]:
     globals()['ax%s' % (i)] = i

But be warned, it's yucky.

pitamer
  • 905
  • 3
  • 15
  • 27
  • OP already has a list containing these values. Why is a dictionary any better? – ChrisGPT was on strike Aug 13 '20 at 16:07
  • Hi @Chris :) Well, for several reasons: **1.** Variables are for storing data - not for being named the data. This is a classic example for a case where a dict will be more simple to work with; **2**. assigning directly to `globals()` or `locals()` is [highly discouraged](https://docs.python.org/3/library/functions.html#locals); **3.** It adds to the global scope in a way that isn't justifiable given that it can be avoided. – pitamer Aug 13 '20 at 16:25
  • I'm not clear on why a dict would be easier to deal with than a list. We're fundamentally talking about a sequence (or possibly set) of values, not keys and values. And points 2 and 3 have literally nothing to do with my comment. – ChrisGPT was on strike Aug 13 '20 at 16:30
  • Oh sorry, I misunderstood - I thought you meant that in this case a dictionary isn't better than dynamically declaring variables, which is what I was trying to convince OP is a bad idea... Actually, I agree that a list would probably be even better than a dictionary here! Haven't thought about it as a solution since it seemed OP wanted to store them separately in the first place. I figured the code in the question is more for example than for actual use case. but it certainly could be the case that even this step isn't necessary. Thanks! :) @Chris – pitamer Aug 13 '20 at 16:38
0

It depends on the scope you want to do it in, you'll probably want to do this on the module, but assuming you just want the local scope, you could:

>>> for i in [1,2,3]:
...     locals()['ax{}'.format(i)] = i
... 
>>> print(ax1, ax2, ax3)
1 2 3

Edit to add, like others have said, that this is probably an anti-pattern, unless you're developing a library that has to implement a lot of magic.

Oin
  • 6,951
  • 2
  • 31
  • 55
  • 1
    What part of [The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter](https://docs.python.org/3/library/functions.html#locals) suggests that this is a reasonable idea? – superb rain Aug 13 '20 at 16:07
  • 2
    Please don't show how to do this without a big fat warning _against_ the practice. – ChrisGPT was on strike Aug 13 '20 at 16:07
0

You can achieve the same purpose by creating a dictionary and then storing the lists as values while storing the names as keys.

for n in range(10):
    axes[f'ax{n}'] = [n]
tetris
  • 21
  • 1
  • 7