Purify


#1

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

When I run it I get an error: Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4].

What's causing this error? The code looks like it should run just fine.

def purify(numbers): 
    for x in numbers: 
        if x % 2 != 0: 
            numbers.remove(x)  
    return numbers    
        



#2

Hi! truelockmage !
Did you notice that in the exercise:
"Do not directly modify the list you are given as input; instead, return a new list with only the even numbers."

def purify(lst_number):
new_list=[]
for i in lst_number:
if i%2==0:
new_list.append(i)
return new_list


#3

I'm coding what you have here basically and I can't get it to work. I've tried several different versions: Declaring newNumbers outside the function. Declaring newNumbers inside the function. Going straight to printing the call to the function after the function. Re-initializing newNumbers to the call to the function. Then, printing newNumbers. I keep getting one of two different errors, either: "Purify can't be found; is not defined" (when I can see that it is and has a colon at the end of the header). Or, "None type object has no attribute 'append'."

I can't see what I'm doing wrong here. The latter error implies a library isn't imported.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def purify(numbersPassed):
newNumbers = []
for n in numbersPassed:
if n%2 == 0:
newNumbers = newNumbers.append(n)
return newNumbers

newNumbers = purify(numbers)

print newNumbers

This is driving me nuts. This should work. I just tried the below (giving new list a new var name oustide the function) as well and got the same "None Type object has no attribute append" error:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def purify(numbersPassed):
newNumbers = []
for n in numbersPassed:
if n%2 == 0:
newNumbers = newNumbers.append(n)
return newNumbers

newDigits = []
newDigits = purify(numbers)

print newDigits


#4

Trying a more old-school, traditional coding approach (using an index instead of list items), and I still can't get it to work:

numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def purify(list_numbers):
i = 0
newList = []
for i in range(0, len(list_numbers), 1):
if list_numbers[i]%2 == 0:
newList = newList.append(list_numbers[i])
return newList

newDigits = []
newDigits = purify(numbers)
print newDigits

Now I'm getting error: "'NoneType' object is not iterable". I'm passing a list; how is it not iterable? Everything I can find in Codecademy reads to pass a list into a function, you just pass the list's name. Is there some datatype or something that needs to passed like in structured OOP languages?

This is like beating my head against a wall. I can do this in Java. Granted there are more lines of code, but at least it works.


#5

Ok, now this is really not making sense. I moved on to the next lesson because for the life of me I can't get Purify to work.

I just passed a list into a function for the next lesson (Product) and it worked fine. This isn't making sense that it's not working in Purify.

Here's what I coded for Product:

integerList = [4,5,5]

def product(integerListPassed):
product = 1
for i in range(0, len(integerListPassed)):
product *= integerListPassed[i]
return product

print product(integerList)

Outputs to the console: 100

This type of For loop should work no problem for Purify as well.


#6

def purify(numbers):
result = []
for i in numbers:
if (i % 2) == 0:
result.append(i)
return result


#7

Thanks a lot! I was doing the same mistake of not making another new list.


#8

I noticed other users came up with the same solution (with exception of the variable names...). I am posting my solution even if the same of other solutions previously posted:

def purify(list_num):
    purified_list = []
    for item in list_num:
        if item % 2 == 0:
            purified_list.append(item)
    return purified_list

#9

def purify(lists):
return[number for number in lists if number%2==0]
You can try by this code,believe me


#10

Seems like CodeAcademy was being picky about just how the list is derived even if it's not modifying the original list in that when the instructions say "do not modify the orignal list" they mean not subtract from the list--even if you copied it to another variable--but to append to an empty list instead.

Don't ".remove(odd numbers)" but ".append(even number)" instead.


#11