Skip to content Skip to sidebar Skip to footer

Removing Negative Values And Printing The Original And The New List

To start of I am telling you this is for school as I am learning to code with Python. Please do explain why I should do something :)! I am looking to learn not just getting the ans

Solution 1:

The "cleanest" way to modify external list will be to change its contents without reassigning - which changes list object reference. You can't remove elements when looping over list, and removing each non-compliant element while iterating over copy is very ineffective.

But you may reassign contents of list without re-assigning list object reference - using slice on the left side of the assignment

defremoveNegatives(listOfIntegers):
    listOfIntegers[:] = filter(lambda x: x >= 0, listOfIntegers)

This code creates new list of non-negative values, and replaces whole content of the external-scope list.

Solution 2:

You aren't modifying global variable l in your function.

I propose this code in Python, which should work correctly:

import random

defremoveNegatives(listOfIntegers):
    return [x for x in listOfIntegers ifnot x < 0]

l = []
for i in xrange(0, random.randint(15,25)): #gives me the random numbers
    l.append(random.randint(-15,15))

print"Before:", l #should only print out the original list of numbers
l = removeNegatives(l)
print"After:", l #should only print out the new list without the numbers that are <0

It's way shorter. What do you think about it?

Solution 3:

Just saw your comment relative to not being able to modify the code below l = []

In that case, you need to reassign to listOfIntegers coming out of your function

defremoveNegatives(listOfIntegers):
    global l
    k = listOfIntegers[:]           #takes a copy of the listfor item in listOfIntegers:     
        if item < 0:                #checks if it is lower than 0
           k.remove(item)
    print k
    l = k

You make a copy of the global as you come in the function, you just need to repoint it to the modified copy as you leave.

Edit: other comments relative to modifying a list while you iterate it are not accurate, as you are not modifying the list you are iterating on, you are modifying the "copy" of the list. While others have offered good suggestions on improving the conciseness of the method, your original method was perfectly valid with the above tweaks.

Edit2: volcano's 'comment' relative to the global is correct, the global statement should be added inside the def to perform it this way. reference volcano's answer for the best approach, but I'll leave this around for the discussion point.

Solution 4:

Since you're studying Python, this is a good place to learn list comprehension:

$ cat /tmp/tmp.py
_list = [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14,
        -2, -9, -14, 2, -10, -5, 8, 7]

print("Before:",_list)
print("After:",[a for a in _list if a >= 0])

$ python3 /tmp/tmp.py
Before: [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14, -2, -9, -14, 2, -10, -5, 8, 7]
After: [2, 7, 13, 13, 5, 11, 10, 5, 0, 2, 8, 7]

As you can see, the elimination of the negative number in the list comprehension stage is concise, clear, and if you test it, you'd find it's faster than the comparable solution using loops.

Post a Comment for "Removing Negative Values And Printing The Original And The New List"