12/15 please help to check


#1

def purify(numbers):
new_num=[]
new_numbers=numbers
for num in numbers:
if num%2!=0:
new_num=new_numbers.remove(num)
return new_num


#2

@cssrockstar07957

The way you are going about it is kinda wonky.

Let's start with the baisics.

What is the problem?

Problem: Remove all odd numbers!

Ok next how do we do that?

if number % 2 == 0:
    print(even)

Ok so we can find our even numbers good. Now we need to collect only the even numbers.

even_numbers = []
for number in numbers:
    if number % 2 == 0:
        even_numbers.append(number)

When you use the method remove it will remove the first entry that it finds not all of them.

So instead of removing entries lets just build a list of the items that meet our quilifications.

list_numbers = [1, 2, 3, 4, 5, 6]
def purify(number_list):
    return_list = []
    for number in number_list:
        if number % 2 == 0:
            return_list.append(number)
    return return_list

It is harder to remove something then it is to make a new list with what you want.

Good luck!


#3

Thank you so much for help me out!


#4

You can also do something like this:

def purify(l):
    for i in l[:]:    # l[:] will create a copy of list you are looping through
        if i % 2 != 0:
            l.remove(i)
    return l