-1

How would I re-write this function using a list comprehension? The resulting function should only have 2 lines: the definition line and the return line containing a list comprehension expression.

def processList(listOfNumbers):
    result = []
    for i in listOfNumbers:
        if i<0:
            result.append(i*i)
        else:
            result.append((i*i)+1)
    return result
Fukiyel
  • 1,166
  • 7
  • 19

1 Answers1

1

You may be trying to do this :

def processList(listOfNumbers):
    return [i ** 2 if i < 0 else i ** 2 + 1 for i in listOfNumbers]
Fukiyel
  • 1,166
  • 7
  • 19
  • Yes, that is exactly what I trying to do. Thanks, I really appreciate your help. I could not figure how to constrain it to just one return line. But now I see, and this also helps me comprehend list comprehensions. – k1lls4n1ty Mar 06 '19 at 19:55