Purify help please


#1



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


Error Message:


When I run my code in the terminal it work?
[4, 4]


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


#2

an index shift occurs which causes the second successive odd number to skipped.

ways to overcome the problem: don't iterate over the same list as you are removing from.

personally, i would recommend the opposite approach : append even numbers to new list


#3

Thank you. I'll reword the code to append. Greatly appreciated, stetim94!

I added an empty string with append.

def purify(x):
    even = []
    for _ in x:
        if _ % 2 == 0:
            even.append(_)
    return even

#4

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