FAQ: Learn Python - Practice Makes Perfect - purify

This community-built FAQ covers the “purify” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise purify:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

A post was split to a new topic: Attempting purify using remove

I have some problems with this exercise, this is the code I used:

for element in lista2:
if element % 2 != 0:
lista2.remove(element)
return lista2

and these the problems:

  1. the code will remove the element from the original list, even if I only said lista2.remove(element)
  2. when input 4 5 5 4, the for istruction would create only 3 iteractions and not 4
  3. it will remove one 5, but not both

I kind of think that somehow the second iteraction (the one that analyses the first 5) removes the second 5 and not the first, therefore the third iteraction goes straight to the 4th element, the 4, missing entirely that there is another 5, but why? and how can I fix it?

I found an identical post here:

it confirms what I though the for cycle was doing, still doesn’t explain why the code remove the element from the original list, though.

the problem occurs when you loop over the same list as you are removing from. Which you do.

thanks, I now solved it, although something is not clear, can I ask you?

  1. why the code was removing the element from the first list and not only from the second, since I specifically said lista2.revome(element) ?

  2. I used another command entirely, but for argument sake, could I loop over the first list and remove from the second list, so that the index problem won’t occur?

thanks.

most likely because you didn’t copy the list. If you did this:

lista2 = lista1

where lista1 is the original list (i have no idea what you named this, given you didn’t include this code). So i am guessing at this point

but that won’t copy the list, you just get two variable referring the same list in memory.

yes, given .remove() will remove the first match found, so it would “work”. Should you do this? No

most likely because you didn’t copy the list. If you did this:
lista2 = lista1

that’s so very true, I didn’t realise that doing that I would not have a copy of the list!!

that is what they say “learning from mistakes!!”

THANK YOU

There something wrong with this entire course.

Every time I got a solution, and I can’t figure out why it is wrong, I press to see the solution, and solution is the exact same. When it is not the same, and I change it to be the same, it still says that it is wrong.

This is driving me crazy. It has happened many times now…

These things can often be fixed. Could you provide one or more specific examples?

def purify(x):
  for number in x:
    if number%2==0:
      print number 
    else:
     x.remove(number)
  return x
print  purify([1,2,3,4]) 

what is the difference between this

def purify(x):
  for number in x:
    if number%2==0:
      print number 
    else:
     x.remove(number)
  return x
print  purify([1,2,3,4,6])

but console write 2 different answer

console :
1 answer :[2, 4]
2 answer:6
[2, 4, 6]

the difference is in the function calls, the first example doesn’t have consecutive even numbers, the second example does, which causes problem.

The inputs are different, why shouldn’t the output be too? What are you really asking? It’s a 2 character difference you have to add more information than that for it to be a meaningful question.
If you think something else should happen, make an argument for why that would be.

Hello, could someone please explain to me why my code is not accepted?

Even when I try purify([1]) it prints an empty list as asked in the error code at the bottom.

I also returned a new list and didn’t modify the given one.

Can it have something to do with my result list not being defined in the function?

call it multiple times

Does anyone know why this is not accepted? I feel like Codecademy expects the code to be written in a certain way and even when written another way that works, it does not accept it. I’m not sure though as I’m quite new to this. Any help is really appreciated :slight_smile:

Neither appending or removing from that list is appropriate. You’re not supposed to add things to it, and remove is both inefficient and doesn’t match what you need here if you consider what it does.

Creating a new list would be simpler than modifying the already existing one in a suitable way, and you probably shouldn’t modify the original anyhow unless your function’s purpose is specifically to modify it – which it isn’t, since you’re returning a result which would be redundant if you modified the original into the result as the caller would then already have the list and there would be nothing new to return.

And yes, that code is wrong, having to do with that append and remove are both the wrong things to do, so, you end up doing the wrong thing overall as well. If you consider what you’re doing then you may also be able to come up with examples of input where that would fail, but you could also try a couple of different inputs and find something where it doesn’t work that way and then read your code to work out what it would end up doing for that.

The reason you get the right output for the one thing you did try is that wrong things can still have overlap with correct ones, like how a broken clock tells the right time twice a day.

Thank you, that has helped a lot. I understand what you mean by creating a new list and simply adding the corresponding numbers to it that meet the criteria of being an even number, thus not amending nor messing around with the original list.

What you would do to modify the original list is to keep track of two locations, one location for writing and one for reading.

You’d move the reading location forward each iteration, and if the read value should be kept, you’d write it at the writing location, and move that forward. After having moved the values to keep to the front of the list, you would resize it by dropping the extra values at the end.

But as is evident from the steps involved in that, you’d be carrying out roughly the same amount of work if you wrote to a new list instead.

There is also a more general function for this, filter, which you could partially apply a function that determines whether a value should be kept to obtain your purify function:

from functools import partial

def even(x):
    return x % 2 == 0

purify = partial(filter, even)