Purify


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/2?curriculum_id=4f89dab3d788890003000096

<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4].

<What do you expect to happen instead?>
I was expecting both "5"s to be removed from the list, but I believe that my code is covering each index once, so when I remove the five at index [1], the next 5 becomes the new index [1] and is skipped over. Is there a way to either remove all of the same values from a list or get my code to revisit the index that it removed?

```python

Replace this line with your code.
def purify(numbers):
pure_list = numbers
for number in pure_list:
if number % 2 == 1:
pure_list.remove(number)
return pure_list

<do not remove the three backticks above>

#2

Does the exercise ask us to use the built-in list.remove() function? If not, then may I suggest trying a different approach.

Do not directly modify the list you are given as input; instead, return a new list with only the even numbers.

It should be noted that your function is modifying the original list.

pure_list = numbers

The one is not a copy of the other, but references that list. A change in one is a change to the other.

>>> list1 = [1,2,3,4,5]
>>> list2 = list1
>>> list2.remove(3)
>>> list1
[1, 2, 4, 5]
>>> 

To make a shallow copy of a list, use slicing. If you have not yet learned about slices, then set the above aside and try a different approach.


#3

It doesn’t specify to use that function; that was the first approach I thought of using. I noticed most others have used the .append() function to add to an empty list and that seems simple enough. I think I will try that so that I don’t modify the original. Thank you


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.