-2

I would like to create a function where one passes a list of numbers and gets back another list where the signs of all numbers are reversed. This is my code:

def reverse_sign_of_nos_in_a_list(list1):
    """ This function reverses sign of numbers
        in a list and returns a list.
    """
    list2 = []
    for num in list1 :
        if num > 0 :
            return list2.append(num * -1)
        elif num < 0 :
            return list2.append(abs(num))
        else :
            return list2.append(num)

print (reverse_sign_of_nos_in_a_list([1,2,3,-1,-2,-3,0]))

The above code shows None as the output. What's the issue?

Natacha
  • 1,132
  • 16
  • 23
Arjun
  • 1
  • 1
  • 2
  • Hi! Welcome to stack overflow. Keep the title matching with your issue/problem/question, and not with your assigned task or goal. This makes easy the search and helps others after. – Natacha Aug 22 '20 at 17:20

7 Answers7

4

The are a couple of things wrong with your code:

  1. You have a return statement in every branch of your code. That means that you will return from the first iteration of the loop no matter what.
  2. list.append modifies the target instance in place. As is conventional in Python for such methods, it returns None, which, combined with #1 means that you always get a return value of None.

There's also something very strange about your code. You flip the sign of a positive number by multiplying by -1. That makes sense. But then you take the absolute value of a negative number. Why? Flipping the sign is the same as multiplying by -1 for negative numbers too. And even for zero.

In fact, you don't even need to multiply by -1. There's already a "flip the sign" operator: unary -.

You can write your function as a single list comprehension:

def reverse_sign_of_nos_in_a_list(list1):
    return [-x for x in list1]
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
1

reverse_sign_of_nos_in_a_list is returning None because list.append() returns None, not appended list.

Following should do the job:

def reverse_sign_of_nos_in_a_list(list1) :
    """ This function reverses sign of numbers
        in a list and returns a list.
    """
    list2 = []
    for num in list1 :
        if num > 0 :
            list2.append(num * -1)
        elif num < 0 :
            list2.append(abs(num))
        else :
            list2.append(num)
    return list2
Chris
  • 29,127
  • 3
  • 28
  • 51
1

list.append() method will append the new element in-place and then will return None. So your function returns that None. Also it will return immediatelly after first element of the original list is processed.

def reverse_sign_of_nos_in_a_list(list1):
    """ This function reverses sign of numbers
        in a list and returns a list.
    """
    list2 = []
    for num in list1:
        if num > 0:
            list2.append(num * -1)
        elif num < 0:
            list2.append(abs(num))
        else:
            list2.append(num)
    return list2

print(reverse_sign_of_nos_in_a_list([1,2,3,-1,-2,-3,0]))

Please, note I keep the code as close as possible to the original code. I would implement the function differently.

buran
  • 13,682
  • 10
  • 36
  • 61
0

You need to append and then return the list.

list2.append(...)
return list2

The reason you are getting None as output is because append is a void-Method that does not return a value. It only alters list2.

Bernhard
  • 1,253
  • 8
  • 18
0

Because list.append(elem) -- adds a single element to the end of the list. Common error: does not return the new list, just modifies the original

You should use similar to:

def reverse_sign_of_nos_in_a_list(list1) :
    """ This function reverses sign of numbers
        in a list and returns a list.
    """
    list2 = []
    for num in list1 :
        if num > 0 :
            list2.append(-num)
        elif num < 0 :
            list2.append(abs(num))
        else:
            list2.append(num)
    return list2

print(reverse_sign_of_nos_in_a_list([1,2,3,-1,-2,-3,0]))
Nikolay Galkin
  • 251
  • 3
  • 5
0

If you want every element of your list reversed, you should remove all the return which cause the function to return prematurely.

Code

def reverse_sign_of_nos_in_a_list(list1) :
    """ This function reverses sign of numbers
        in a list and returns a list.
    """
    list2 = []
    for num in list1 :
        if num > 0 :
            list2.append(num * -1)
        elif num < 0 :
            list2.append(abs(num))
        else :
            list2.append(num)
    return list2

print (reverse_sign_of_nos_in_a_list([1,2,3,-1,-2,-3,0]))
0

A different, shorter (and kind of more pythonic) way for the whole thing:

def reverse_sign_of_nos_in_a_list(list1):
    return [(-1)*ii for ii in list1]
kushy
  • 356
  • 2
  • 14