I'm struggling to figure out exactly what this assignment is asking me to output. I built this python function but it isn't doing what the professor wants it to do. I'm very confused.
Here is the instructions:
define the function extract_negatives: The first argument, xs, contains integers. We will be removing any negative integers we find in xs, and appending them to the second argument, new_home. A reference to the list that received negatives must be returned. When no second argument is given, a list of all the extracted negatives should be created and returned.
Figuring out the signature of this function is part of the task. Careful: What default value do you want to use for new_home? What happens when we call the function multiple times in the same coding session? (try it out) Go ahead and use methods like .insert(), .pop(), .append()
You might need to iterate and update the list at the same time. Hint: if you need to traverse a list from front to back, but you don't always want to go to the next index location, while loops can be very useful – we get to control when (which iteration) to step forward.
Here are examples given:
input: xs = [1,-22,3,-44,-5,6,7] extract_negatives(xs) yeilds: [-22, -44, -5] #return a list of negatives xs
[1, 3, 6, 7] #remove negatives from xs
Here is the function I built:
def extract_negatives(xs,new_home):
new_home=[]
for num in range(len(xs)):
if xs[num] <0:
new_home.append(xs[num])
return new_home
I have tried asking the professor but it's been a few days with no response. Mayybe you can help make sense of what is being asked?