12/15 Why doesn't this work?


#1
def purify(numlist):
newlist = numlist
for i in range(len(numlist)):
    if not numlist[i]%2 == 0:
        newlist.remove(numlist[i]) 
return newlist

On purify([4,5,5,4]) I end up with an index error when i=3 on line four, must be for numlist[i]. But this code doesn't modify numlist, only newlist, so why would I get an index error there? I've already seen the solution using .append(), just want to understand why this doesn't work.


#2

Well there are a multitude of solves for any problem in programming that is the magical thing about it.

As always when trying to solve a problem programmatically you need to think of what you are trying to do/solve. In this case we and to remove all ODD numbers and keep only numbers that are divisible by 2!

Problem: Only want even numbers from a list

The next thing you need to think about is how do you solve your problem, to which there can be many answers. In this case we just want to loop through our list and get the items and check to see if they are even and if so return them to a stored data type.

Though your code is slightly convoluted.

if not numlist[i]%2 == 0:

This is just not gonna work the way you want it to. Now on to the multiple solves,

EXAMPLES!

def purify(list_please):
    return [item for item in list_please if item % 2 ==0]

def purify(list_please):
    hold = []
    for item in list_please:
        if item % 2 == 0:
            hold.append(item)
    return hold

def purify(list_please):
    hold = []
    x = 0
    while x <= len(list_please):
        try:
            if list_please[x] % 2 ==0:
                hold.append(list_please[x])
        except IndexError:
            pass
        x += 1
    return hold

There are other ways to do this also, these are good ways but they also may not be the best depending on how large your list is.


#3

Thanks, I understand these solutions, but I'm still racking my head for why my original code won't work.

"Though your code is slightly convoluted.

if not numlist[i]%2 == 0:

This is just not gonna work the way you want it to."

Could you go into a little more detail here? Where does it result in an index error?


#4

Go to the interpreter and do this,

a = 2

not a % 2 == 0
# OUTPUT: True

a % 2 == 0
# OUTPUT: False

Now you see why it is convoluted, your code is not doing what you want it to thus it is a bug.

Also,

newlist.remove(numlist[i])

This is just not a good way to go about creating a new list.


#5

I'm getting what I expected:

not a % 2 == 0 returns False

a % 2 == 0 returns True

So for " not numlist[i] % 2 == 0 " why wouldn't it always return True for an odd number?


#6

This is even

a % 2 == 0 returns True

This is not even

not a % 2 == 0 returns False

Also it is just plain terribly bad way to program, especially in python.

Start programming like you would speak, this is how you do most programming in python till you learn the short cuts and tricks. If it does not make sense then it is probably a bad way to go about it.


#7

I'm still not really getting it.

I wanted to check for odd numbers so I could remove them.

Doesn't (not a % 2 == 0) return True for odd values of a?

I can accept that it is a bad way to program, but I'd like to know why it is bad, so I can get better.


#8

I think my code does make sense when I am speaking it. Copy list to separate list (so that I am not for looping through a list I am modifying), check if a list item is odd, if it is, remove it from the list. That all makes sense to me.

The only dumb thing I've realized was doing the "i in range(len(numlist))" instead of "item in numlist"