Purify


#1

iam able to work with my code as in image


but this code doesnot work when i replace if by while condition why i dont understand
please
some body help me


#2

Hi @archanaprathapan ,

You could implement a while loop, if you want, but it would not be a good idea to use it to increment an index to the list, li, within the for loop, when that loop is also being used to iterate through the list. Either use a for loop or a while loop to iterate through the list, and use an if condition within the loop to check for even numbers.

It is better to use a for loop for this exercise than to use a while loop. If you use a while loop, you need to add a statement that updates the index. You also need to use the index to retrieve items from the list. With the added statements, such a solution is less concise than one that uses a for loop, but here it is, anyway ...

def purify(li):
    results = []
    ind = 0
    while ind < len(li):
        if not li[ind] % 2:
            results.append(li[ind])
        ind += 1
    return results
    
print purify([1, 7, 2, 3, 9, 8, 4, 5, 0, 6])

Output ...

[2, 8, 4, 0, 6]

The point of using a for loop for this exercise is to avoid having to deal with indexes.