Unless there is an overwhelming need to create a mess of variable names, I would just use a dictionary, where you can dynamically create the key names and associate a value to each.

a = {}
k = 0
while k < 10:
    # dynamically create key
    key = ...
    # calculate value
    value = ...
    a[key] = value 
    k += 1

There are also some interesting data structures in the collections module that might be applicable.

Answer from JoshAdel on Stack Overflow
Discussions

Dynamic variable names
You might think you want this but you really don't. Not having predictable variable names at runtime is a recipe for broken code. https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_make_variable_variables.3F More on reddit.com
🌐 r/learnpython
6
2
May 17, 2021
Dynamic variable names - is there a better way?
Yes, use a list or a dictionary. EDIT: For instance, in this case, day_count = 3 event_label = "Conference" event_days = [] for day in range(day_count): event_days.append(event_label) for event in event_days: print(event) This could of course be further condensed: day_count = 3 event_label = "Conference" event_days = [ event_label for _ in range(day_count) ] print('\n'.join(event_days)) More on reddit.com
🌐 r/learnpython
27
25
August 29, 2022
generating variable names on fly in python - Stack Overflow
Is there a way I can generate variable names in python in a loop and assign values to them? For example, if I have prices = [5, 12, 45] I want price1 = 5 price2 = 12 price3 = 45 Can I do this in ... More on stackoverflow.com
🌐 stackoverflow.com
November 6, 2016
Trying to create a loop to create dynamic variables
globals()[f"server{i}"] Don't do that though. Use a dictionary, or a list. More on reddit.com
🌐 r/learnpython
7
1
January 23, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-create-dynamically-named-variables-from-user-input
Python program to create dynamically named variables from user input - GeeksforGeeks
July 23, 2025 - Dynamic_Variable_Name = "geek" # The value 2020 is assigned # to "geek" variable exec("%s = %d" % (Dynamic_Variable_Name, 2020)) # Display variable print(geek) ... Here we are using the vars() method for creating dynamically named variable and ...
🌐
Sololearn
sololearn.com › en › Discuss › 3015787 › how-to-create-a-dynamic-variable-in-python
How to create a dynamic variable in python | Sololearn: Learn to code for FREE!
Edward Marais , to do this task we can generate the variable names like *var* + a number inside a loop. then we have to add this name together with the value we like to assign to the locals() dict.
🌐
DEV Community
dev.to › chintanonweb › beyond-static-embracing-dynamic-variable-creation-in-python-57ol
Beyond Static: Embracing Dynamic Variable Creation in Python - DEV Community
April 8, 2024 - One way to dynamically create variables in Python is by utilizing the globals() function. This function returns a dictionary representing the current global symbol table, which contains all the variables defined in the global scope.
🌐
Python Forum
python-forum.io › thread-21151.html
dynamically create variables' names in python
May 14, 2021 - Hi guys, i want to create variables in the following way: assign a name (e.g. var1), then add the name to the prefix of the variable: name = 'var_1' this_is_+name = pd.DataFrame()the outcome i would l
🌐
Reddit
reddit.com › r/learnpython › dynamic variable names
r/learnpython on Reddit: Dynamic variable names
May 17, 2021 -

Hi,

Is there a way to create variable dynamically through a loop?

For instance: aList = [1, 2, 3, 4, 5, 6]

I want all odd numbers in their own set of variables like 1o = [1] 1e = [2] 2o = [3] 2e = [4] And so on

What I’ve tried:

AList = [1, 2, 3, 4, 5, 6] num = 1

For i in aList: If i % 2 == 0: List(num, ‘e’).append(i) Else: List(num, ‘o’).append(i)

I get that I need an assignment somewhere but I can’t figure it out

Thanks!

🌐
Delft Stack
delftstack.com › home › howto › python › python dynamic variable name
Python Dynamic Variable Name | Delft Stack
December 14, 2023 - The for loop, combined with the globals() function, offers a powerful way to create dynamic variable names. The for loop in Python is an iterative tool that allows you to repeat a block of code a specified number of times.
Find elsewhere
🌐
LinkedIn
linkedin.com › pulse › dynamic-ways-creating-variables-python-akhil-pathirippilly-mana
Dynamic ways of creating variables in python
February 11, 2022 - So If you are planning to use this just to create local dynamic variable , this is not the method for you. But yes, globally you can do as follows (You need to make sure you are not overwriting any global variables which are already loaded by system or program unintentionally. ) 3. Using setattr built-in method: setattr(obj, name, value) will call self.__setattr__ dunder method and will set value assigned to "name" argument as attribute name and value assigned to "value" argument as its original value.
🌐
Esri Community
community.esri.com › t5 › python-questions › create-dynamic-variable-from-returned-value › td-p › 1185872
Solved: Create dynamic variable from returned value / vari... - Esri Community
July 7, 2022 - You can use __getattr__() and __setattr__() built in methods on global/local namespaces to get/set variables by string name on an object or module. See: https://docs.python.org/3.7/library/functions.html#getattr and https://docs.python.org/...
🌐
Reddit
reddit.com › r/learnpython › dynamic variable names - is there a better way?
r/learnpython on Reddit: Dynamic variable names - is there a better way?
August 29, 2022 -

numDays will not be hardcoded like in the example below, but will change according to user input.

Example of what I'm trying to do:

numDays = 2;

eventLabel = "Conference"

event_day1 = ' '

event_day2 = ' '

event_day3 = ' '

i = 0

while i <= numDays:

event_day(i+1) += eventLabel;

i += 1

print(event_day1)

print(event_day2)

print(event_day3)

Expected output:

Conference

Conference

Conference

EDIT: Thank you so much for the responses! They were very helpful!

🌐
Codingdeeply
codingdeeply.com › home › python: 5 best techniques to generate dynamic variable names
Python: 5 Best Techniques to Generate Dynamic Variable Names - Codingdeeply
February 23, 2024 - An illustration of how to make a dynamic variable name with the globals() Method is shown below: prefix = "dynamic_" suffix = "_variable" var_num = 1 # Creating dynamic variable name using globals() globals()[prefix + str(var_num) + suffix] ...
🌐
CodeSpeedy
codespeedy.com › home › how to create dynamic variable name in python
How to create dynamic variable name in Python - CodeSpeedy
September 18, 2021 - # Use a for loop to create dynamic variable names and assign values for x in range(0, 7): variable_name = f”variable1{x}” variable_value = f”Hello CodeSpeedy Student {x}!!!” dynamic_variables[variable_name] = variable_value
🌐
MP4Moviez
pakainfo.com › home › python › python dynamic variable name – how to create a dynamic variable name in python?
Python Dynamic Variable Name - How To Create A Dynamic Variable Name In Python? - Pakainfo
for i in range(0, 9): globals()[f"my_variable{i}"] = f"Welcome from variable number {i}!" print(my_variable3) # Welcome from variable number 3! for x in range(0, 9): globals()['string%s' % x] = 'Welcome' # string0 = 'Welcome', string1 = 'Welcome' ... string8 = 'Welcome' name = "a" value = True player_message = {name: value} print(player_message["a"]) Don’t Miss : How To Read And Write Files In Python 3? I hope you get an idea about python dynamic variable name.
🌐
Medium
medium.com › geekculture › a-cool-way-to-dynamically-create-variables-in-python-7c20c12f4f23
A Cool Way To Dynamically Create Variables In Python | by Liu Zuo Lin | Geek Culture | Medium
April 17, 2023 - # {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x1060c9a10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/lzl/Documents/repos/test/a.py', '__cached__': None, 'a': 100, 'b': 200, 'c':300, 'd':400} ^ if we set more variables, they appear in the globals() dictionary too.
🌐
Plain English
plainenglish.io › home › blog › python › how to dynamically declare variables inside a loop in python
How to Dynamically Declare Variables Inside a Loop in Python
July 18, 2021 - globals() is a dictionary which contains all the global variables with the variable name as its key and value as its value. Declaring variables using globals is the same as declaration using a dictionary. The output is the same as the first one. One of the greatest features of Python is its support for OOP (Object-Oriented Programming). We shall get to the use of that amazing property to declare variables dynamically.
🌐
Quora
quora.com › How-can-I-dynamically-create-variables-in-Python
How to dynamically create variables in Python - Quora
Answer (1 of 5): x = 0 For i in range(10): String = “var%d = %d”%(x, x) exec(String) x+=1 Now you have 11 variables
🌐
Quantifiedcode
docs.quantifiedcode.com › python-anti-patterns › maintainability › dynamically_creating_names.html
Dynamically creating variable/method/function names — Python Anti-Patterns documentation
Dynamically creating variable/method/function names · View page source · Sometimes a programmer gets an idea to make his/her work easier by creating magically working code that uses setattr() and getattr() functions to set some variable. While this may look like a good idea, because there is no need to write all the methods by hand, you are asking for trouble down the road.