2

I have a function to check for the "negative", "positive" and "zero" value in the list. Below is my function:

def posnegzero(nulist):
    for x in nulist:
        if x > 0:
            return "positive"
        elif x < 0:
            return "negative"
        else:
            return "zero"

But when I run this function, it stops after checking the value of the first number in the list. For example:

>>> posnegzero([-20, 1, 2, -3, -5, 0, 100, -123])
"negative"

I want it to continue for the entire list. In the above function, if I change every instance of return to print, then it does what it should but now I don't want it to say None when the function is complete. Any ideas of where I went wrong?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Aystealthy
  • 169
  • 1
  • 16

4 Answers4

12

return stops the control flow of your function and returns back the flow. You may use yield here which will convert your function into a generator. For example:

def posnegzero(nulist):
    for x in nulist:
        if x > 0:
            yield "positive"
        elif x < 0:
            yield "negative"
        else:
            yield "zero"

It will yield the next result every time next() is called on the returned object:

>>> result = posnegzero([-20, 1, 2, -3, -5, 0, 100, -123])
>>> next(result)
'negative'
>>> next(result)
'positive'
>>> next(result)
'positive'

Or you may get all the result at once as:

>>> result = posnegzero([-20, 1, 2, -3, -5, 0, 100, -123])
>>> list(result)
['negative', 'positive', 'positive', 'negative', 'negative', 'zero', 'positive', 'negative']

You can also iterate it using for loop. for loop repeatedly calls the next() method until it receives a StopIteration exception. For example:

for result in posnegzero([-20, 1, 2, -3, -5, 0, 100, -123]):
    print(result)

# which will print
negative
positive
positive
negative
negative
zero
positive
negative

For more information on yield, please refer: What does the “yield” keyword do?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
4

Your issue is returning immediately on the first list element

Personally, I would do like this - define the function for a value only. Not a list. Run the function over each value of the list

(Python 3)

def posnegzero(x):
    if x > 0:
        return "positive"
    elif x < 0:
        return "negative"
    else:
        return "zero"

print(list(map(posnegzero, [-20, 1, 2, -3, -5, 0, 100, -123]))) 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You could always just build your results in a list:

def posnegzero(lst):
    result = []

    for x in lst:
        if x > 0:
            result.append("positive")
        elif x < 0:
            result.append("negative")
        else:
            result.append("zero")

    return result

Which works as follows:

>>> posnegzero([-20, 1, 2, -3, -5, 0, 100, -123])
['negative', 'positive', 'positive', 'negative', 'negative', 'zero', 'positive', 'negative']

Or even with a conditional list comprehension:

def posnegzero(lst):
    return ["positive" if x > 0 else "negative" if x < 0 else "zero" for x in lst]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

Try this

d=["pos" if i>0  else "neg"if i<0  else "zero" for i in [-20, 1, 2, -3, -5, 0, 100, -123]]

output

['neg', 'pos', 'pos', 'neg', 'neg', 'zero', 'pos', 'neg']

Artier
  • 1,648
  • 2
  • 8
  • 22