Python: Removing Negatives From A List Of Numbers
Solution 1:
It's generally a bad idea to remove elements from a list while iterating over it (see the link in my comment for an explanation as to why this is so). A better approach would be to use a list comprehension:
num_list = [item for item in num_list if item >= 0]
Notice that the line above creates a new list and assigns num_list
to that. You can also do an "in-place" assignment of the form
num_list[:] = ...
which does not create a new list in memory, but instead modifies the memory location already being pointed to by num_list
. This difference is explained in more detail here.
Solution 2:
Much simpler:
>>>a = [ 1, 2, 3, -3, 6, -1, -3, 1]>>>[x for x in a if x >= 0 ]
[1, 2, 3, 6, 1]
If you really do want to loop, try this:
defremove_negs(num_list):
r = num_list[:]
for item in num_list:
if item < 0:
r.remove(item)
print r
This does what you want:
>>> remove_negs([ 1, 2, 3, -3, 6, -1, -3, 1])
[1, 2, 3, 6, 1]
The key is that the assignment statement r = num_list[:]
makes a copy of num_list. In order not to confuse the loop, We then remove items from r
rather than from the list we are looping over.
More: Python's treatment of variables is a bit subtle. Python keeps variable names, like r
or num_list
separate from variable data, such as [1, 2, 3, 6, 1]
. The names are merely pointers to the data. Consider the assignment statement:
r = num_list
After this statement is run, r
and num_list
both point to the same data. If you make a change to r
's data, you are also making a change to num_list
's data because they both point to the same data. Now, consider:
r = num_list[:]
This statement tells python to modify num_list
's data by taking only certain elements of it. Because of this, python makes a copy of num_list
's data. It just so happens that [:]
specifies that we want all of num_list
's data unchanged but that doesn't stop python from making a copy. The copy is assigned to r
. This means that r
and mum_list
now point to different data. We can make changes to r
's data and it doesn't affect num_list
's data because they have different data.
If this is new to you, you might want to look at this tutorial about python's approach to variable names and variable data: Understanding Python variables and Memory Management
Examples:
>>>a = [ 1, 2, 3, -3, 6, -1, -3, 1]>>>b = a # a and b now point to the same place>>>b.remove(-1) >>>a
[1, 2, 3, -3, 6, -3, 1]
Contrast with:
>>>a = [ 1, 2, 3, -3, 6, -1, -3, 1]>>>b = a[:] # a and b now point to different data>>>b
[1, 2, 3, -3, 6, -1, -3, 1]
>>>b.remove(-1)>>>b
[1, 2, 3, -3, 6, -3, 1]
>>>a
[1, 2, 3, -3, 6, -1, -3, 1]
Solution 3:
Another solution
filter( lambda x: x>0, [ 1, 2, 3, -3, 6, -1, -3, 1])
[1, 2, 3, 6, 1]
Solution 4:
From a comment on arshajii's answer:
but that's removing the negative numbers. i need the negative signs removed but still keep the number in the list.
Removing the negative numbers is exactly what your code is clearly trying to do, and it's also the only way to get the desired result:
THe result is suppose to be [1, 2, 3, 6, 3, 1]
But if you really want to "remove the negative signs" from the numbers, that's even easier. For example, to remove the negative sign from -3
, you just negate it and get 3
, right? You can do this in-place, as in your existing code:
for index, item in enumerate(num_list):
if item < 0:
num_list[index] = -item
… or in a list comprehension, as in arshajii's:
num_list = [-item if item < 0 else item for item in num_list]
And it's even easier with the abs
function, which does exactly that—negates negative numbers, leaves positive and zero alone:
num_list = [abs(item) for item in num_list]
Either way, of course, this will give you [1, 2, 3, 3, 6, 1, 3, 1]
, which is the wrong answer… but if your comment is correct, it's the answer you asked for.
Solution 5:
Other the the conventional variable operator non-variable
Try some yoda conditions too =)
>>> [i for i in x if 0 <= i][1, 2, 3, 6, 1]
Post a Comment for "Python: Removing Negatives From A List Of Numbers"