Purify


#1



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


Oops, try again. Your code looks a bit off--it threw a "list index out of range" error. Check the Hint if you need help!


Why doesn't work? I have no idea


def purify(numbers):
    new_numbers = []
    for i in numbers:
        if numbers[i] % 2 == 0:
            numbers.remove(i)
        else:
            new_numbers.append(i)
    return new_numbers
    
purify([1,2,3])


#2

When you use a for loop, you iterate through each item in the list. The variable that you used as i will be each item in the list, so if there is a number in the list that's higher than the length of the list, the way you wrote it, it will try to find a number at an index that's not there. Just check if i is even and not numbers[i].


#3

Thanks so much. :slight_smile:


#4

def purify(numlist):
purlist = []
for i in range(len(numlist)):
if int(numlist[i]) % 2 == 0 and int(numlist[i]) >= 2:
purifylist.append(numlist[i])
return purifylist


#5

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