Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Removing negatives from a list of numbers

The question is to remove negatives from numbers.

When remove_negs([1, 2, 3, -3, 6, -1, -3, 1]) is executed, the result is: [1, 2, 3, 6, -3, 1]. The result is suppose to be [1, 2, 3, 6, 3, 1]. what is happening is that if there are two negative numbers in a row (e.g., -1, -3) then the second number will not get removed. def main(): numbers = input("Enter a list of numbers: ") remove_negs(numbers)

def remove_negs(num_list): 
  '''Remove the negative numbers from the list num_list.'''
    for item in num_list: 
        if item < 0: 
           num_list.remove(item) 

    print num_list

main()
like image 204
user3161743 Avatar asked Jan 06 '14 21:01

user3161743


People also ask

How do you take a negative array in Python?

negative() function is used when we want to compute the negative of array elements. It returns element-wise negative value of an array or negative value of a scalar. Parameters : arr : [array_like or scalar] Input array.

How do you convert a negative number to a positive in Python?

The Python abs() method returns the absolute value of a number. The absolute value of a number is the number's distance from 0. This makes any negative number positive, while positive numbers are unaffected. For example, abs(-9) would return 9, while abs(2) would return 2.


2 Answers

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.

like image 96
arshajii Avatar answered Sep 21 '22 17:09

arshajii


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:

def remove_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]
like image 34
John1024 Avatar answered Sep 21 '22 17:09

John1024