-2

Why am I only receiving every second element in the list here?

I tried the pop() method with the index of the sandwich in the first loop, but it gave me the same problem. Only every second element gets printed.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Jan 23 '23 at 20:04
  • See also: https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating. You don't want to remove elements from a list while you are iterating over that list. – MrFlick Jan 23 '23 at 20:05

1 Answers1

0

You're modifying the sandwich_orders list while iterating over it, which is messes up your iteration. Since there aren't any conditions there anyway, I'd finish the iteration and then just assign an empty list to it: sandwich_orders = [], or clear it: sandwich_orders.clear().

Or better yet, since you aren't using this list after the loop is done, just leave it be.

Mureinik
  • 297,002
  • 52
  • 306
  • 350