Python Forum
Changing a variable's name on each iteration of a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing a variable's name on each iteration of a loop
#1
Is it possible in python to change the name of a variable on each iteration of a loop? For example:
for i in range(10):
    variableNameToChange+i="iterationNumber=="+str(i)
I know this won't work, and you can't assign to an operator, but how would you change / add to the name of a variable on each iteration of a loop, if it's possible?
Reply
#2
You would be creating a debugging nightmare. But, if I was going to try such an abomination I would break out my copy of the Necronomicon and write code that writes itself. In more detail. Write a section of code that writes the modified Python code to a file. Then import the file and execute the code, which writes the next iteration to the file, imports, then executes, indefinitely. Possible endless loops that involve your hard drive, all kinds of odd things can happen. Black magic.
Reply
#3
(Jan-03-2020, 01:18 AM)jefsummers Wrote: You would be creating a debugging nightmare. But, if I was going to try such an abomination I would break out my copy of the Necronomicon and write code that writes itself. In more detail. Write a section of code that writes the modified Python code to a file. Then import the file and execute the code, which writes the next iteration to the file, imports, then executes, indefinitely. Possible endless loops that involve your hard drive, all kinds of odd things can happen. Black magic.
are you suggesting the use of the "open()" function?
Reply
#4
(Jan-03-2020, 01:06 AM)rix Wrote: Is it possible in python to change the name of a variable on each iteration of a loop? For example:
for i in range(10):
    variableNameToChange+i="iterationNumber=="+str(i)
I know this won't work, and you can't assign to an operator, but how would you change / add to the name of a variable on each iteration of a loop, if it's possible?

You can totally make variable names dynamically. Python is infinitely reflective.

In this case you do not need to dig so deep though.

Just make a dictionary, use the variable names as keys and the intended. Then when you created all of the "variables" that you need, just update them to the locals "dictionary"(symbol table).

var_holder = {}

for i in range(10):
    var_holder['my_var_' + str(i)] = "iterationNumber=="+str(i)

locals().update(var_holder)

print(my_var_0)
Please note this solution does not require black magic, a unicorn, or an enchanted debugger.
Reply
#5
(Jan-03-2020, 03:41 AM)Clunk_Head Wrote:
(Jan-03-2020, 01:06 AM)rix Wrote: Is it possible in python to change the name of a variable on each iteration of a loop? For example:
for i in range(10):
    variableNameToChange+i="iterationNumber=="+str(i)
I know this won't work, and you can't assign to an operator, but how would you change / add to the name of a variable on each iteration of a loop, if it's possible?

You can totally make variable names dynamically. Python is infinitely reflective.

In this case you do not need to dig so deep though.

Just make a dictionary, use the variable names as keys and the intended. Then when you created all of the "variables" that you need, just update them to the locals "dictionary"(symbol table).

var_holder = {}

for i in range(10):
    var_holder['my_var_' + str(i)] = "iterationNumber=="+str(i)

locals().update(var_holder)

print(my_var_0)
Please note this solution does not require black magic, a unicorn, or an enchanted debugger.
I do not quite understand if you understand what I meant, or maybe I'm not advanced enough to completely understand what the code you have included in your reply does, but either way, this is a lot more complicated than I thought, and I only learned that dictionaries existed yesterday. Doh Maybe I'll just have to keep trying to understand it or become more familiar with dictionaries and then try and finish my task of trying to change a variable's name on each iteration of a loop.

Maybe the code you've sent does actually technically create variables with different names on each iteration, but it's not exactly what I was looking for, but I'll try to make it work as I was trying to do.
Reply
#6
(Jan-03-2020, 03:52 AM)rix Wrote: I do not quite understand if you understand what I meant, or maybe I'm not advanced enough to completely understand what the code you have included in your reply does, but either way, this is a lot more complicated than I thought, and I only learned that dictionaries existed yesterday. Doh Maybe I'll just have to keep trying to understand it or become more familiar with dictionaries and then try and finish my task of trying to change a variable's name on each iteration of a loop.

Maybe the code you've sent does actually technically create variables with different names on each iteration, but it's not exactly what I was looking for, but I'll try to make it work as I was trying to do.

What are you trying to do?
Why do you need to change variable names at each iteration of a loop?
Do the variables already exist?
Reply
#7
If you are in learning process then you should learn this: don't create variables dynamically. Why? Presumably you want to access them dynamically as well.

If you need to create values dynamically use some other Python data structure: dictionary (as suggested by Clunk_Head) or namedtuple or list or something else.

You can learn how to create variables dynamically and why it's considered 'shooting yourself in the foot' from Why you don't want to dynamically create variables
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Excel Cell As A Variable In A Loop knight2000 7 1,074 Aug-25-2021, 12:43 PM
Last Post: snippsat
  saving each iteration of a loop sgcgrif3 3 1,577 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Using Excel Cell As A Variable In A Loop knight2000 7 1,477 Jul-18-2021, 10:52 AM
Last Post: knight2000
  Simple Variable Saving in Loop DevDev 3 1,352 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  While Loop Variable Freezing? stylingpat 13 2,326 Feb-25-2021, 10:42 AM
Last Post: Abdullah
  Change variable value during a while loop? penahuse 2 1,520 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Variable in for loop samuelbachorik 4 1,514 Jul-21-2020, 08:45 AM
Last Post: ndc85430
  Need help with for loop and variable value substitution in a function rsurathu 2 1,191 Jul-21-2020, 06:47 AM
Last Post: rsurathu
  String slicing and loop iteration divyansh 9 2,570 Jun-07-2020, 10:29 PM
Last Post: divyansh
  calling a variable in a for loop in windows dwaynes 1 983 Apr-02-2020, 05:21 PM
Last Post: mcmxl22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020