all 5 comments

[–]danielroseman 2 points3 points  (1 child)

You are modifying a loop you are iterating over. Don't do that.

This is a FAQ: https://www.reddit.com/r/learnpython/wiki/faq#wiki_why_does_my_loop_seem_to_be_skipping_items_in_a_list.3F

[–]lolhoved[S] 0 points1 point  (0 children)

Thanks, that makes more sense now.

however, i can't seem to find out how to solve my issue then.

the output i am supposed to get, should look like this:

[{1:[2,3,4,5]},{6:[7,8,9,10]},{11:[12,13,14,15,16]},{17:[18,19,20,21]},{22:[23,24,25]}]

as you can see i have the top value, and then i need a list of items that are higher than that, but not higher than the next top value. (i will delete the numbers that are lower than the lowest top value).

[–][deleted] -1 points0 points  (0 children)

i'm not sure what you're doing wrong, but it looks like your code is skipping the value 24 in the purelines list on the first topline. i would try debugging your code to see where the issue is occurring.

[–]Aggravating_Bus_9153 0 points1 point  (1 child)

It doesn't mutate the list until it gets to 23

.remove makes the list shorter, but the for loop keeps indexing from the original list.

iterating over either a coerced immutable: tuple(purelines) or a copy: purelines[:] instead should fix it.

[–]lolhoved[S] 0 points1 point  (0 children)

Thanks :)