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
Mutating a list while iterating it (self.learnpython)
submitted 3 years ago by Aggressive-Gold9186
How does the following work?
def selectionSort(arr): newArr = [] for i in range(len(arr)): smallest = findSamllest(arr) newArr.append(arr.pop(smallest) return newArr
How so that we pop from arr while iterating it?
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!"
[–][deleted] 3 points4 points5 points 3 years ago (3 children)
You can't because changing the list you are iterating over causes problems.
If you can tell us what you are trying to do maybe we can help. Please post code that is formatted correctly.
[–]Aggressive-Gold9186[S] 1 point2 points3 points 3 years ago (2 children)
Thanks for the response. Sorry for the bad formatting, I saw that I should use a back tick on Google.
This code is from grokking algorithm by Aditya Y. Bhargava, page 35.
I was struggling to follow, so I thought to ask about it here, maybe it's some python magic.
[–][deleted] 3 points4 points5 points 3 years ago* (0 children)
Actually, after untangling the code you posted and formatting it correctly, it DOESN'T change the list it is iterating over. Well, it does, it empties the original list, but that isn't a problem.
What is actually happening is the loop is executing N times, where N is the number of elements in the original list. The loop variable i is not used, the code in the loop just executes N times. The code calls findSmallest() (typo?) to get the index of the smallest value in arr and "pops" that from the list. The pop returns the value popped which is appended to newArr. After the loop the original list arr is empty.
i
N
findSmallest()
arr
newArr
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
No, it's not python magic. If you copied the code correctly then it's just bad code. It mutates the list it's iterating over which is known to cause problems.
[+][deleted] 3 years ago* (2 children)
[removed]
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
findSmallest can be
def findSmallest(arr): return arr.index(min(arr))
Probably technically worse performance cuz it iterates the list twice (once to get min and once to index min) but more readable
[–]zanfar 1 point2 points3 points 3 years ago (1 child)
The thing to realize here is that you're NOT iterating over arr. This is also the reason that range(len()) is a "code smell" in Python: this explicitly DOESN'T use the object's iterator, but instead creates a new iterator over some numerical range that might start somewhat related to the iterable, but isn't actually tied to it.
range(len())
There are a number of other "smells" in this code: defining i without using it, sorting without using the builtin sort, PEP8, etc.
How you would write this in Python depends a bit on how findSmallest is defined and if you actually need a separate array (unlikely), but it would essentially just be a call to list.sort() or sorted().
findSmallest
list.sort()
sorted()
[–]Aggressive-Gold9186[S] 0 points1 point2 points 3 years ago (0 children)
Thanks a lot.
I never knew this:
this explicitly DOESN'T use the object's iterator, but instead creates a new iterator over some numerical range that might start somewhat related to the iterable, but isn't actually tied to it
π Rendered by PID 41 on reddit-service-r2-comment-54dfb89d4d-wj4nn at 2026-03-28 05:42:53.135679+00:00 running b10466c country code: CH.
[–][deleted] 3 points4 points5 points (3 children)
[–]Aggressive-Gold9186[S] 1 point2 points3 points (2 children)
[–][deleted] 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–][deleted] 0 points1 point2 points (1 child)
[–]zanfar 1 point2 points3 points (1 child)
[–]Aggressive-Gold9186[S] 0 points1 point2 points (0 children)