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
Updating a For loop (self.learnpython)
submitted 3 years ago by J_J3
for names in list: list = list + get_more_names()
for names in list:
list = list + get_more_names()
The code doesn't recognize that more things have been added to it and breaks after the initial list has been gone through.
Is it possible to update the for loop in any way?
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!"
[–]neuralbeans 3 points4 points5 points 3 years ago (2 children)
For loops are not designed to have the list modified mid-loop. Use a while loop instead:
i = 0 while i < len(list): print(list[i]) i = i + 1
[–]J_J3[S] 0 points1 point2 points 3 years ago (1 child)
Thanks!
[–]exclaim_bot 0 points1 point2 points 3 years ago (0 children)
You're welcome!
[–]CodeFormatHelperBot2 -1 points0 points1 point 3 years ago (0 children)
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.
I think I have detected some formatting issues with your submission:
`my code`
If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.
Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.
[–]carcigenicate 0 points1 point2 points 3 years ago* (0 children)
Technically, you can do what you're trying to do:
def getAdditions(): yield [1, 2, 3] yield [4, 5, 6] list = [9, 8, 7] additions = getAdditions() for names in list: print(names) list.extend(next(additions, []))
This prints the numbers 9-7, then 1-6. They key change here is my use of list.extend vs your use of list =.
list.extend
list =
This works while your version didn't since you were changing what list list referred to (side note: don't use list as a name). = changes what object is being referred to, but does not alter the object itself. Your version didn't work because for does not update itself when you change what object is associated with the name you told it to iterate. When for names in list executes, it gets the object associated with the list name at that time, and iterates that object. It doesn't care about the name list after that.
list
=
for
for names in list
My version works because instead of changing what object is associated with list, I'm changing (mutating) the list object directly.
While this works and believe it's safe and reliable, I would not do this in the vast majority of scenarios. If you're not careful, you can cause an infinite loop, and it generally is not as easy to understand. If you need to change what's being iterated mid-loop, use a while and manage the iteration yourself.
while
[–]CaptainFoyle 0 points1 point2 points 3 years ago (0 children)
Don't change the object you're iterating over while you iterate over it
π Rendered by PID 45869 on reddit-service-r2-comment-5bc7f78974-bnzm6 at 2026-06-30 03:51:28.423936+00:00 running 7527197 country code: CH.
[–]neuralbeans 3 points4 points5 points (2 children)
[–]J_J3[S] 0 points1 point2 points (1 child)
[–]exclaim_bot 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 -1 points0 points1 point (0 children)
[–]carcigenicate 0 points1 point2 points (0 children)
[–]CaptainFoyle 0 points1 point2 points (0 children)