You probably want a dict instead of separate variables. For example

d = {}
for i in range(3):
    d["group" + str(i)] = self.getGroup(selected, header+i)

If you insist on actually modifying local variables, you could use the locals function:

for i in range(3):
    locals()["group"+str(i)] = self.getGroup(selected, header+i)

On the other hand, if what you actually want is to modify instance variables of the class you're in, then you can use the setattr function

for i in group(3):
    setattr(self, "group"+str(i), self.getGroup(selected, header+i)

And of course, I'm assuming with all of these examples that you don't just want a list:

groups = [self.getGroup(i,header+i) for i in range(3)]
Answer from Eli Courtwright on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › changing-variable-names-with-python-for-loops
Changing Variable Names With Python For Loops - GeeksforGeeks
July 23, 2025 - In this example, below Python code below adds the prefix "var_" to each variable name in the list original_names. It uses a for loop to iterate through the original names, creates new names with the prefix, and appends them to the list prefixed_names.
Discussions

Changing variable name in for loop?
The python-pptx library requires that I name each new slide something different How? A library shouldn't have access to the variable names you're creating. I looked at the docs but couldn't find anything. Can you provide an example? More on reddit.com
🌐 r/learnpython
5
2
May 2, 2022
Changing a variable name in a for loop?

"I want to define several variables with trailing ascending numbers 0, 1, 2, etc." is a red flag that you really should be using a list.

More on reddit.com
🌐 r/Python
8
0
June 15, 2016
python - How to have a variable that changes its name after every loop? - Stack Overflow
So basically I want to create a variable that changes after every iteration of a for loop to be the same as the search term that is used in the for loop in question, is that possible? I explained b... More on stackoverflow.com
🌐 stackoverflow.com
python - How do you create different variable names while in a loop? - Stack Overflow
For example purposes... for x in range(0,9): string'x' = "Hello" So I end up with string1, string2, string3... all equaling "Hello" More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › changing variable name in for loop?
r/learnpython on Reddit: Changing variable name in for loop?
May 2, 2022 -

Hello,

I know this is probably a really basic python task, but I have been unable to figure it out. I am using python-pptx to create a powerpoint. My actual script is more complex, but for the sake of this question, suppose I have a for loop with which I iterate on a list, and create a new slide for each item in the list. The python-pptx library requires that I name each new slide something different - how do I update the name of the variable for each loop iteration? In the example below, how would "slide1" become "slide2" as the loop progresses through the list?

list_of_slide_content = ["example","of","content"]
for cont in list_of_slide_content: 
    slide1 = pptx.slide(0)
    slide1.add_text(cont)
🌐
Quora
quora.com › How-can-I-change-the-name-of-a-variable-in-a-loop-in-Python
How to change the name of a variable in a loop in Python - Quora
Answer (1 of 13): Hello! I have a good and bad news for you. I start with the bad one. Producing a new variable name at each iteration of a for loop isn’t a good idea. So I will give you a way to use Python `list` instead, to achieve the same thing. But now the good one: what you wanted to do a...
🌐
Iditect
iditect.com › faq › python › changing-variable-names-with-python-for-loops.html
Changing variable names with Python for loops
This code dynamically renames loop variables within a loop using enumeration to represent different fruits. "Python change variable names in loop based on condition"
🌐
Python Forum
python-forum.io › Thread-Changing-a-variable-s-name-on-each-iteration-of-a-loop
Changing a variable's name on each iteration of a loop
March 1, 2020 - 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 y...
🌐
Sololearn
sololearn.com › en › Discuss › 3029838 › change-variable-name-at-every-iteration
Change variable name at every iteration
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
Find elsewhere
🌐
OARC Stats
stats.oarc.ucla.edu › spss › faq › how-can-i-write-a-python-program-to-rename-variables-in-spss
How can I write a Python program to rename variables in SPSS? | SPSS FAQ
Once you have saved the file as a Python file (with the .py extension), you can use the SPSS program below to call the function. Because renamefun is just a function (rather than a complete module), you need to use the from option on the import statement. begin program. from renamefun import renamefun import spss, spssaux renamefun("D:\data\elemapi2.sav", "grad_sch", "enroll", "_myvar") end program. For more information looping through lists of variables using Python, please see SPSS FAQ: How can I loop through two lists of variables?
🌐
Stack Overflow
stackoverflow.com › questions › 64199647 › how-to-have-a-variable-that-changes-its-name-after-every-loop
python - How to have a variable that changes its name after every loop? - Stack Overflow
with open ('lista1.txt','r') as file_1: reader_0 = file_1.readlines() # Reads a list of searchterms, # the first search term of this list is "gt-710". for search in reader_0: file_0 = search.replace("\n","") +".txt" file_1 = str(file_0.strip()) try: #if the file named the same as the searchterm exists, read its contents file = open(file_1,"r") search = file.readlines() # How do I create a variable that # changes names? for example I want the # content of file readlines be saved in # a variable called the same as the # searchterm in this ase I want it to # be gt-710 = file.readlines()...in the
🌐
Sololearn
sololearn.com › en › Discuss › 1604705 › how-do-i-rename-a-variable-in-python
[SOLVED] how do i rename a variable in python? | Sololearn: Learn to code for FREE!
November 30, 2018 - In Python, whenever we do a = b, the id of a equals that of b. But this might be the best we can hope for. Thanks again! 😊 ... If Assigning and deleting , or editing cant help its hard to answer cause you can't rename variables directly , using dict or list can help u
🌐
Stack Overflow
stackoverflow.com › questions › 69617845 › is-there-a-way-to-edit-a-variable-with-changing-names-in-a-loop
python - Is there a way to edit a variable with changing names in a loop? - Stack Overflow
This changing variable needs to be named FY-0, name change to FY-1... is already included. import os import pandas as pd in_path = 'ESG_untransfored' out_path = 'ESG_transformed' if not os.path.exists(out_path): os.mkdir(out_path) for filename in os.listdir(in_path): if filename.endswith('.xlsx'): df = pd.read_excel(os.path.join(in_path, filename)) df = df.iloc[2:] duf=duf.rename(columns = {'Unnamed: 29':'FY-1'}) duf=duf.rename(columns = {'Unnamed: 30':'FY-2'}) duf=duf.rename(columns = {'Unnamed: 31':'FY-3'}) duf=duf.rename(columns = {'Unnamed: 32':'FY-4'}) duf=duf.rename(columns = {'Unnamed:
🌐
Reddit
reddit.com › r/learnpython › how to change variable name in loop
r/learnpython on Reddit: How to change variable name in loop
December 29, 2021 -

I have a large data frame I am creating smaller data frames with. Basically, there is one giant data frame with different departments information, so, I am creating smaller data frames, all the rows labeled purchasing will go into a smaller data frame, engineering, accounting, etc.

I created a while loop to go through the length of the large data frame and want to assign each smaller data frame with it's own name. Code might explain it better. It is below.

n=0

while n < len(df_dept_list.index):

dept = df_dept_list.iloc[n]

df_dept = df_all_data [
        (df_all_data['Department'] == dept)].dropna()

 n = n + 1

I would like to just have the data frame name change each iteration like df_dept0, then df_dept1, something that changes with the value 'n'.

Any ideas how to?

🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Iteration › WPNamingVariablesinForLoops.html
7.10. 👩‍💻 Naming Variables in For Loops — Foundations of Python Programming
While these two tips won’t always apply, they are general best practices when it comes to choosing variable names. Below we have an example! Since we can’t see the contents of x right now - this might be the case if we have a long program - then these names would make the program hard to understand.
🌐
Quora
quora.com › How-do-you-create-different-variable-names-while-in-a-loop-in-Python
How to create different variable names while in a loop in Python - Quora
Answer (1 of 5): There is a way: [code]>>> exec("A = 3") >>> A 3 [/code]… meaning in a for loop you could synthesize some varnames and make them globals. However said practice is heavily frowned upon. Best practice is to create a dict and instead of a plethora of names polluting your namespace,...
🌐
Runestone Academy
runestone.academy › ns › books › published › foppff › more-about-iteration_naming-variables-in-for-loops.html
7.7 Naming Variables in For Loops
Here are some tips to make your for loops more readable for yourself and anyone else who may read your programs: ... Use singular nouns for the iterator variable, which is also called the loop variable (things like “song”, “book”, “post”, “letter”, “word”).