Reputation: 13
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 answer.
I am trying to get rid of the negative items in the list. I want to print the list Before (including the negative items) and after ( without the negative items of course). My problem is that it prints out the original list and the new list without negative items on the Before print and the original one on After. Like this:
Before: [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14,
-2, -9, -14, 2, -10, -5, 8, 7]
[2, 7, 13, 13, 5, 11, 10, 5, 0, 2, 8, 7]
After: [2, 7, -3, -3, 13, -14, 13, 5, 11, -4, 10, 5, 0, -5, -14, -2, -9,
-14, 2, -10, -5, 8, 7]
This is what I've done and I just can't seem to figure out what I should do...
import random
def removeNegatives(listOfIntegers):
l = listOfIntegers[:] #takes a copy of the list
for item in listOfIntegers:
if item < 0: #checks if it is lower than 0
l.remove(item)
print l
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
removeNegatives(l)
print "After:", l #should only print out the new list without the numbers that are <0
Upvotes: 0
Views: 2252
Reputation: 6185
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.
Upvotes: 0
Reputation:
You aren't modifying global variable l
in your function.
I propose this code in Python, which should work correctly:
import random
def removeNegatives(listOfIntegers):
return [x for x in listOfIntegers if not 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?
Upvotes: 1
Reputation: 476
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
def removeNegatives(listOfIntegers):
global l
k = listOfIntegers[:] #takes a copy of the list
for 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.
Upvotes: 0
Reputation: 3582
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
def removeNegatives(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.
Upvotes: 1