Purify


#1




Your function fails on purify([4, 5, 5, 4]).


It returns [4, 5, 4] when it should return [4, 4].


    for x in nums:
        if x % 2 == 1:
            nums.remove(x)
    return nums


#2

remove is a bad way to solve this problem because of index shift, try instead to append even numbers to a new list.

do i need to explain what index shift is, and why it makes it troubling to solve this problem with remove?


#3

I can do that with index shift,but it is not possible with remove?


#4

what does this mean?

saying it is not possible would be lying, but it is far more difficult and not efficient.


#5

As in, I want to do it with remove. I have already done by shifting index


#6

i don't think you understand what i mean what index shift, lets look at this list:

[4,5,5,4]

agree with me that the index are: 0, 1, 2 and 3?

the loop arrives at index 0, the number is 4, so nothing gets removed
the loop arrives at index 1, the number is 5, 5 gets removed

Now it gets interesting. lists can't have empty indexes, so the 5 at index 2 goes to index 1, and the 4 from index 3 goes to index 2

now the loop at arrives at index 2, the number is 4, so nothing gets removed

so the second 5 gets skipped because of the index shift that occurs because of remove.

so how would you solve this problem with remove? (i advise against this), either loop over the list multiply times to remove all sequential odd numbers

use range in the for loop to get indexes, an if condition to check if the number is odd, if so, add a while loop to remove all sequential odd numbers, use try except to catch the index error


#7

Oops, try again. Your code looks a bit off--it threw a "not all arguments converted during string formatting" error. Check the Hint if you need help!.....what to do for this error ??here is my code----

def purify(l):
new=[]
for i in l:
if i%2==0:
new.append(i)
return new
ip=raw_input("input list :")
print purify(ip)


#8

how did you think this:

ip=raw_input("input list :")

would work?

even if you then enter a list:

input list :[4,5,5,4]

the input would stored as string:

print type(ip)

it won't work. I recommend not to use raw_input


#9

thanks for the reply...


#10

it is not that you can not use raw_input, i should have added a bit more detail.

you can make a list, then use a loop to prompt the user for numbers, use int() to convert the raw_input to integer, then append to list, you just need to figure how to make the loop stop


#11

Since you cannot directly modify the list. The best way to do it is to create a new empty list and add the even numbers to it. Then return the new list.
You can test the numbers by:

if nums % 2 == 0

Hope this helps!


#12

2 posts were split to a new topic: Purify


#13

Hello All,
i have the following issue:
my code looks like this:
valami2=[]
def purify(numbers):
for i in numbers:
if i%2==0:
valami2.append(i)
return valami2

if i run it in PyCharm for example, it works perfectly, but i get the error message on codeacademy that:
"Oops, try again. Your function fails on purify([1]). It returns [2, 4, 6, 8] when it should return []."

Test shows that with the mentioned input it gives back []. What can be the root cause?


#14

the exercise needs to verify your function works correctly, so it calls the function multiply times with different list. because you declare the empty list outside your function, the list stays with the remainders of the previous function call

When you run in pycharm, you call the entire script, the validition script on codecademy only calls the function


#15

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