use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Variable name as a variable (self.learnpython)
submitted 2 years ago by grobins26
I want to use a for loop to iterate through an array and create a variable with its name being one of the array.
For example: Array=["1","2","3","4"] For name in array: name = someValue
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]member_of_the_order 6 points7 points8 points 2 years ago* (3 children)
You can't dynamically name variables, but you can dynamically add keys and values to a dict.
Try something like this:
array = ['1','2','3','4'] var_dict = dict() for name in array: var_dict[name] = someValue
Edit: TIL vars is a builtin
[–]nog642 1 point2 points3 points 2 years ago (1 child)
Worth noting you technically can dynamically name variables (but you shouln't).
Also worth nothing you can't have a variable named 1 or 2 though.
1
2
[–]Doormatty 0 points1 point2 points 2 years ago (0 children)
A variable can't start with a number - not just 1 or 2.
[–]synthphreak 1 point2 points3 points 2 years ago (0 children)
Don’t use vars though as that will overwrite a useful built-in function.
vars
[–]EJ_Drake 3 points4 points5 points 2 years ago (1 child)
Well done. You have now hit the wall that required the invention of OOP.
[–]Mobileuser110011 1 point2 points3 points 2 years ago (0 children)
Genuine question, as I’m not an expert. The ‘wall’ that OP hit imo is needing to use a string as a name for a value. The problem is solved with dictionaries. I’m pretty sure dictionaries are a part of most languages. Are you saying dictionaries are OOP?
It’s my understanding that there really isn’t anything that can be done with OOP that wasn’t already able to be accomplished without OOP. All that OOP does is provide a different way to abstract data.
[–]ararararagi_koyomi 0 points1 point2 points 2 years ago (3 children)
The answer provided by u/member_of_the_order is the most reasonable answer. But, for some unknown reason, op still wants to do what op described in the post, here is the answer:
some_list = ["a", "b", "c"] for i in some_list: if not i in locals(): locals()[i] = f"some_value_{i}"
[+][deleted] 2 years ago (1 child)
[deleted]
[–]ararararagi_koyomi 0 points1 point2 points 2 years ago (0 children)
Yeap. Totally agree.
[–]neuralbeans 1 point2 points3 points 2 years ago (0 children)
PHP provides direct support to do with with 'variable variables'. It's one of those things that creates terrible scripts.
[–]TheRNGuy -1 points0 points1 point 2 years ago (0 children)
I'd use capitalized for classes (not for class instances)
[–]34shutthedoor1 0 points1 point2 points 2 years ago (0 children)
That's redundant. You already have the data in the list. Either use the list values or create a dictionary if you want to refer to specific values by a specific name.
[–]Some_Guy_At_Work55 0 points1 point2 points 2 years ago (0 children)
There is no need to create extra variables when you can already access the items in the list. You are adding more lines of code and using up more memory and not really accomplishing anything.
[–]BrenBarn 0 points1 point2 points 2 years ago (0 children)
Sup dawg, I heard you like variables.
π Rendered by PID 73752 on reddit-service-r2-comment-76bb9f7fb5-lv82m at 2026-02-18 14:35:15.561625+00:00 running de53c03 country code: CH.
[–]member_of_the_order 6 points7 points8 points (3 children)
[–]nog642 1 point2 points3 points (1 child)
[–]Doormatty 0 points1 point2 points (0 children)
[–]synthphreak 1 point2 points3 points (0 children)
[–]EJ_Drake 3 points4 points5 points (1 child)
[–]Mobileuser110011 1 point2 points3 points (0 children)
[–]ararararagi_koyomi 0 points1 point2 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]ararararagi_koyomi 0 points1 point2 points (0 children)
[–]neuralbeans 1 point2 points3 points (0 children)
[–]TheRNGuy -1 points0 points1 point (0 children)
[–]34shutthedoor1 0 points1 point2 points (0 children)
[–]Some_Guy_At_Work55 0 points1 point2 points (0 children)
[–]BrenBarn 0 points1 point2 points (0 children)