0

Here is my script:

# I have 100 variables
x0 = 3.14
x1 = 2.72
x2 = 1.41
x3 = 2.33
.... (omit this part)
x100 = 7.77

# xi corresponds to the value that the index i of a list needs to subtract, 
# now I want to loop through the list
for i in range(100):
    lst[i] -= 'x{}'.format(i)

This clearly won't work, since the variable is not a string. So how should I string formatting a variable?

4
  • You should seriously rethink your design. Why having 100 variables, when you can have a single variable referring to a list and access each value by index? Even if it's generated code, then better generate a list! Commented May 9, 2020 at 12:12
  • I understand what you are saying. But this is an over-simplified version. In my real case, I need to use this string-formatting (if possible) trick to avoid an additional for loop. If there are better solutions, I would not ask this question. Commented May 9, 2020 at 12:32
  • Yes, you can be sure there are better solutions. And what's wrong with having an additional for if it helps you avoid having 100 variables? This looks like an XY problem, you're asking something to get around a problem, but the real issue is different - and we don't know all of the context to be able to effectively help you. Commented May 9, 2020 at 12:38
  • I understand this may look like an XY problem, but I am acutually a computational chemist and I am using some Python packages particularly for my field. It doesn't make any sense if I put my actual script here. Commented May 9, 2020 at 13:06

3 Answers 3

1

You can access these variables using locals:

lst[i] -= locals()['x{}'.format(i)]
Sign up to request clarification or add additional context in comments.

1 Comment

This locals() function works fine. I will accept your answer since your answer is not finding another way around and don't use eval() which seems to be a bad practice.
1

You should instead use a list here.

x = [...] (where x has a len of 100)

Then for your loop:

for i in range(100):
    lst[i] -= x[i]

(Renamed list to lst to avoid name collision with the built-in type)

3 Comments

This is not the answer I am looking for. This script is oversimplified, and for my real case, the x1 to x100 are also all variables, and is generated on-the-fly. So basically my purpose is to update the list[i] AS SOON AS the xi is generated.
if the variables are dynamically generated and assigned, and you want to update list[i] as soon as the corresponding x value is generated, you should look into using iterators or generators. Point is, having this many variables when you could have just one containing/generating all the values is going to be very hard to maintain.
The OP is asking for a technique to solve their specific problem. @Arhiliuc Cristina gave a good answer. Whether using ‘eval()‘ is a good idea is a different story.
1

In order to get the value of the variable, you can use Python's eval function

eval('x{}'.format(i))

And please, don't ever call your list variable list.

Edit: While this solution works in this case, it is recommended to avoid eval as much as possible because it allows code injections in a way you wouldn't expect.

1 Comment