-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
  • 1
    Ugh, @MartinWettstein, please don't encourage this. – Chris Aug 13 '20 at 16:02
  • 2
    Why do you want to do this? It's definitely an antipattern and an [XY problem](https://meta.stackexchange.com/q/66377/248627); this is why lists and other compound data structures exist in the first place. You already have `liste`, so why do you think you need `ax1`, `ax2`, and `ax3`? – Chris Aug 13 '20 at 16:03
  • Because then I want do to axi=plt.subplot(xxi) (x are int) – EqElahin Aug 13 '20 at 16:19
  • And why can't `ax` be a list of three values (which is what you already have in `liste`)? – Chris Aug 13 '20 at 16:23

3 Answers3

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,276
  • 2
  • 30
  • 54
  • 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. – Chris Aug 13 '20 at 16:07
0

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
  • 643
  • 2
  • 12
  • 25
  • OP already has a list containing these values. Why is a dictionary any better? – Chris 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. – Chris 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

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