# Python loop exercise help

**URL:** <https://discuss.python.org/t/python-loop-exercise-help/49348>\
**Category:** Python Help\
**Tags:** help\
**Created:** [March 24, 2024, 9:34am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348 "2024-03-24T09:34:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eddsauceplz](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@Eddsauceplz](https://discuss.python.org/u/Eddsauceplz)\
**Post date:** [March 24, 2024, 9:34am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/1 "2024-03-24T09:34:29Z")

</div>

Hi, I am new to coding.  
Why does my code still slice the list when it gets to 11, despite it being an odd number?  
The function should be removing any even number from the front of the list  
Intended output should be [11, 12, 15]

```python
def delete_starting_evens(my_list):
 while len(my_list) > 0:
  for num in my_list:
    if num % 2 == 0:
      my_list = my_list[1:]
    else:
      my_list[0:]
  return my_list

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

```

Output:  
[12, 15]

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 24, 2024, 10:09am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/2 "2024-03-24T10:09:42Z")

</div>

The code modifies a mutable container, whilst iterating over it. Don’t do that. This is bad practise, and inevitably difficult to reason about, and can even be tricky to implement a simple goal with.

In general, to modify a container in place, making an iteration of an index over a range is usually more predictable. If memory is not a concern, list comprehensions are great. It’s better to use native list methods like `.remove` than making slices from a memory point of view too (but tracking indices and items is a little trickier then).

In this situation in particular, consider the converse, what you want to return, not what you want to delete: a simple search for the first odd number and a slice from there onwards is all you need.

Also that while loop adds nothing over an if statement and another return, and is asking for trouble.

Anyway I think the problem could be because when you start the for loop, `iter` is called on `my_list`, making an iterator over the original list items. This iterator is not updated when you subsequently reassign the name `my_list` to subslices of the new list it currently points to. So the for loop always iterates over all the numbers, and chops off the first number in my\_list whatever that is, whenever an even number is found in the original list. There are 6 numbers, 4 of which are even, so the 1st 4 elements are chopped off one at a time, leaving the last 2.

But as I said, it’s difficult to reason about (notoriously so) so I could be wrong. My idea could be tested by assigning `my_list` to anything else, e.g. a string or an empty tuple, and seeing if the inner loop still iterates over each element of the original list.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 24, 2024, 10:31am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/3 "2024-03-24T10:31:21Z")

</div>

> [@JamesParrott](#):
>
> Anyway I think the problem could be because when you start the for loop, `iter` is called on `my_list`, making an iterator over the original list items. This iterator is not updated when you subsequently reassign the name `my_list` to subslices of the new list it currently points to. So the for loop always iterates over all the numbers, and chops off the first number in my\_list whatever that is, whenever an even number is found in the original list. There are 6 numbers, 4 of which are even, so the 1st 4 elements are chopped off one at a time, leaving the last 2.
> 
> But as I said, it’s difficult to reason about (notoriously so) so I could be wrong.

You’re absolutely right that it’s hard to reason about 🙂 This isn’t exactly what happens, but that only strengthens your point that mutating the thing you’re iterating over makes it hard to figure out what’s happening.

> [@JamesParrott](#):
>
> In this situation in particular, consider the converse, what you want to return, not what you want to delete: a simple search for the first odd number and a slice from there onwards is all you need.

Yes, and importantly, doing this with a single slice is not just faster, it’s much much easier to think about.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 24, 2024, 11:08am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/4 "2024-03-24T11:08:15Z")

</div>

Thanks :). I’m sure there’re subtleties I’ve overlooked, but I can’t figure out what they are:

```python
def delete_starting_evens(my_list):
  for num in my_list:
    print(f'{num=} {my_list=}')
    if num % 2 == 0:
      my_list = my_list[1:]    
  return my_list

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

```

```python
num=4 my_list=[4, 8, 10, 11, 12, 15]
num=8 my_list=[8, 10, 11, 12, 15]
num=10 my_list=[10, 11, 12, 15]
num=11 my_list=[11, 12, 15]
num=12 my_list=[11, 12, 15]
num=15 my_list=[12, 15]
[12, 15]

```

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 24, 2024, 11:29am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/5 "2024-03-24T11:29:22Z")

</div>

This is an example of a different way to mess up loops like this. It could be called the “pointer issue” (or “repeated off-by-one issue”). It naively mutates a container destructively, but the iterator does not account for the elements removed from it as OP would perhaps intend (it’s possible to manually adjust an index, by tracking an offset and adding that to it, but that rapidly leads to messy code):

```python
def delete_starting_evens(my_list):
  for num in my_list:
    print(f'{num=} {my_list=}')
    if num % 2 == 0:
      my_list.remove(num)
    else:
      break
  return my_list

```

```python
num=4 my_list=[4, 8, 10, 11, 12, 15]
num=10 my_list=[8, 10, 11, 12, 15]
num=12 my_list=[8, 11, 12, 15]
[8, 11, 15]

```

---

<div class="post-metadata">

**Author:** ![FelixLeg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/felixleg/32/18439_2.png) [@FelixLeg](https://discuss.python.org/u/FelixLeg)\
**Post date:** [March 24, 2024, 11:40am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/6 "2024-03-24T11:40:20Z")

</div>

I would do it like that: instead of iterating over a list and _modifying it on the fly_ (which, as James pointed out, is a really, _really_ bad thing) I would first found where is the last even number from the start of a list and then chop off a _whole block_ of the list:

```python
def delete_starting_evens(my_list):
   last = -1
   for i in range(len(my_list)):
      if my_list[i] % 2 != 0:
           last = i
           break
   if last > 0:
      #chop!
      del my_list[:last]
   return my_list

```

now it works:  
`[11, 12, 15]`

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [March 24, 2024, 11:49am UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/7 "2024-03-24T11:49:08Z")

</div>

> [@Rosuav](#):
>
> > [@JamesParrott](#):
> >
> > Anyway I think the problem could be because when you start the for loop, `iter` is called on `my_list`, making an iterator over the original list items. This iterator is not updated when you subsequently reassign the name `my_list` to subslices of the new list it currently points to. So the for loop always iterates over all the numbers, and chops off the first number in my\_list whatever that is, whenever an even number is found in the original list. There are 6 numbers, 4 of which are even, so the 1st 4 elements are chopped off one at a time, leaving the last 2.
> > 
> > But as I said, it’s difficult to reason about (notoriously so) so I could be wrong.
> 
> You’re absolutely right that it’s hard to reason about 🙂 This isn’t exactly what happens, but that only strengthens your point that mutating the thing you’re iterating over makes it hard to figure out what’s happening.

You sure? This sounds exactly right to me, what difference are you seeing? (ofcourse, the fact that we are still disagreeing proofs the point even more xD)

---

<div class="post-metadata">

**Author:** ![Eddsauceplz](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@Eddsauceplz](https://discuss.python.org/u/Eddsauceplz)\
**Post date:** [March 24, 2024, 12:26pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/8 "2024-03-24T12:26:06Z")

</div>

I am so new to coding half the stuff you’ve written has flown over my head. But I think I kind of get it. Instead of making a function modify my initial argument, it’s better to try and make the function take out parts of the argument and formulate an output that way?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 24, 2024, 1:36pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/9 "2024-03-24T13:36:53Z")

</div>

> [@MegaIng](#):
>
> You sure? This sounds exactly right to me, what difference are you seeing? (ofcourse, the fact that we are still disagreeing proofs the point even more xD)

Actually, looks like I was misreading part of the OP’s code, and there’s a line in it that does absolutely nothing. So you WERE right. In any case, the solution was the same: don’t iterate and mutate.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 24, 2024, 5:09pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/10 "2024-03-24T17:09:59Z")

</div>

Generally the advice is to try and iterate over fixed predictable things.

But If possible, yes, I would also prefer returning new copies to modifying in place, especially via a function with side effects. That leads to much cleaner, functional style code, and testable functions.

If if it also needed to work on lists that take up more than half of the available memory, it can’t be copied in memory, so the list should be mutated in place. In resource constrained environments though, is Python really the best choice in the first place?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 24, 2024, 5:16pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/11 "2024-03-24T17:16:55Z")

</div>

> [@JamesParrott](#):
>
> If if it also needed to work on lists that take up more than half of the available memory, it can’t be copied in memory, so the list should be mutated in place. In resource constrained environments though, is Python really the best choice in the first place?

Bearing in mind that two lists referring to the same items aren’t duplicating those items, it would have to be a pretty insanely big list for that to be a concern.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [March 24, 2024, 6:08pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/12 "2024-03-24T18:08:23Z")

</div>

> [@JamesParrott](#):
>
> The code modifies a mutable container, whilst iterating over it.

That’s not true. And the end of your very same post shows that you know it. Confusing.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 24, 2024, 6:33pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/13 "2024-03-24T18:33:14Z")

</div>

Indeed. That’s what the code attempts, not what it does. And is more a general description of what not do do, in order to avoid associated bugs.

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [March 24, 2024, 8:16pm UTC](https://discuss.python.org/t/python-loop-exercise-help/49348/14 "2024-03-24T20:16:25Z")

</div>

[

> [@Eddsauceplz](#):
>
> I am so new to coding half the stuff you’ve written has flown over my head. But I think I kind of get it. Instead of making a function modify my initial argument, it’s better to try and make the function take out parts of the argument and formulate an output that way?

Very often defining the problem will give very strong hint to the solution. This problem can be defined: “take slice from given list starting from first non-even number till end”. The solution to the problem lies in finding first non-even number and its index for making slice. Python has built in function [enumerate](https://docs.python.org/3/library/functions.html#enumerate) which can be used. This is apparently homework therefore just an idea:

```python
>>> sample = [4, 8, 10, 11, 12, 15]
>>> for i, num in enumerate(sample):
... if num % 2 != 0:
... print(sample[i:])
... break
...
[11, 12, 15]

```
