Purify exercise

Hi, i’m doing the purify function, why these code doesn’t work? The exercise is about creating a function to remove odds in a list. I checked with a stepper and seems that the loop for jumps a item on the list after execute the else option

def purify(l):
  nl=l
  for i in nl:
    if i%2 == 0:
      pass
    else:
      nl.remove(i)
  return nl

Thank youu!

removing from the same list as you are looping over is problematic.

this:

nl = l

just creates a variable which points to the same list in memory. It doesn’t make a copy

ooh thank you!! I see my error

There are very few situations where using pass is appropriate. You would for example not write code like this:

pass
pass
pass
print('hi')
pass
pass

(doing nothing is not something you want to include in your code, it is not something you need to express)

do this instead:

if odd:
    remove

or more likely:

if even:
    keep

because the task is not to remove something, but to create a separate thing with only some of the elements. you’re creating a new value, not modifying something that already exists.

??
what do you propose? your message is not helping xd

Having code that does nothing (pass) doesn’t bring you closer to your goal, so I’m proposing you remove that code. Forget that pass exists.

And I’m pointing out that the task is to build a new value, so removing might not be what you’re looking for, rather, you’re building something new based on something old.

Removing is also a whole lot of work, a single removal is as much work as re-creating the whole list. It’s probably not what you meant.

hey thank you mate, i’m just learning, not necessary to throw that ironic message, are you a pro? congrats, i’m just on the starting point.
after seteim94 message i came up with:
def purify(l):
evens=
for i in l:
if i%2 == 0:
evens.append(i)
return evens

you could be nicer ionatan
and remember “no te vayas pa’lo hondoo…”

No need to be rude :c