[12/15 purify](http://discuss.codecademy.com/t/12-15-purify/20255):


Continuing the discussion from 12/15 purify:

def purify(ls):
    for i in range(len(ls)):
        if ls[i]%2!=0:
        return ls
print purify([1,2,3,4])

why it has error IndexError: list index out of range??


@yunyousanren: That's because you're trying to access a position that doesn't exist.
If you have a list with five integers:

listA = [1,2,3,4,5]

And you try to print the element at position 5 by doing print listA[5], you'll get an error, informing such position doesn't exist, index is out of range.



Because you exceed the limit of your list? At the the beginning, your items are indexed at 0,1,2 and 3, make sense right? However, the moment you delete an item, your index is going to shift, because now you are items indexed at 0,1,2 so you will exceed the index (since range still want to go to index 3, which no longer exist.

This is also a problem, the exercise is going to test this: purify([4,5,5,4]), due to the shift in index i just explained, this will only remove on of the odd numbers, not both. Also the exercise says:

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

So, i recommend to change your code so you append odd numbers to a new list, rather then removing them from your current list


uhm.. g4be range seems not to exceed the limits of the list:

def purify(ls):
    for i in range(len(ls)):
        print i

Or did i miss something?


thank you , i got it , /I just want to make sure it use this method if it can work to ge


thank you , i got it , but if we can limit it to the modified list length?


I can only think about the fact he was removing the elements and trying to access something so that would cause the error.
Not sure if removing the elements from the list would cause the range() function, which is using the len() function, to automatically alter it's value.

At least, that's what I could think of.


I believe you would only be able to alter the list of values obtained from using range and len is by calling the function again and again, a.k.a. recursion.
But try to get a copy from your original list, other than using the original, like @stetim94 mentioned!


@g4be, I missed have missed it, but i didn't get the impression from your answer that out of range was caused by the removal of items.

@yunyousanren, i doubt you can get it to work, i will have to see, but if it works, it won't be easy, appending even numbers to a new list is much easier


@stetim94: That's because I forgot to mention about it, my bad!


I just completed the exercise with the del function, but trust me when i say it is not easy (you could say difficult), i absolutely do NOT recommend it


Hi pythonistas,

I found it to be easier by just appending the desired elements ( even numbers ) to a new list, rather than struggling to delete the odd ones. Invite just the cool kids to the party :smiley:

# Define your own terms, how big you want the list to be and what numbers
nlist = []
n = input("How many numbers in the list: ")
count = 1
while n > 0:
    x = input("Number " + str(count) + ": ") 
    n -= 1
    count += 1
print nlist
def purify(nlist):
    z = []
    for i in nlist:
        if i % 2 == 0:
    return z
print purify(nlist)

Good luck!