+ 1

Can you create a dynamic variable in python?

basically what i want to know is if you can create a variable that can create new variables to store data for example ( Human = 0 ) and lets say it was in a for loop that runs n times that n number is how many human variables that will be created and stores data in the loop.

12th Apr 2022, 9:02 AM
Edward Marais
Edward Marais - avatar
5 Answers
+ 3
is this not answered? why don't you continue in this? https://www.sololearn.com/discuss/3015787/?ref=app edit: your question itself unclear and confusing.. you can directly use human1=1 human2=2 .. manually.... what else you expecting by means of run time? do you want automatically create?
12th Apr 2022, 9:08 AM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 Sir, I think he implied the need for a composite object like a dictionary object, which can process key-value pairs. This will solve the problem in my opinion.
13th Apr 2022, 12:29 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Edward Marais variables are stored as dict in the globals(), so maybe something like this? But this is a hack. read more about locals() and globals() https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/built-in/globals#:~:text=Python%20Global%20Keyword-,Python%20globals(),%2C%20methods%2C%20classes%2C%20etc. https://code.sololearn.com/c2SiuCgS5rV1/?ref=app
13th Apr 2022, 9:59 AM
Bob_Li - avatar
+ 1
Sanjay Kamath yes. . But about all those dictionaries and about locals() and globals() , @Lother posted details in OP previous question which I linked.. So question is not clear to me because of again asking... OP resonpse may clarifies it.. Thanx for informing..
13th Apr 2022, 12:19 PM
Jayakrishna 🇮🇳
+ 1
I think you're pointing to ward is create data storage
14th Apr 2022, 12:52 AM
Nuredin Abdala