FAQ: Learn Python - Practice Makes Perfect - purify

faq

#1

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!


#2

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!

#3

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


#4

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?


#5

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.


#6

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


#7

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.


#8

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


#9

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