-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?

1
  • 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, 2020 at 17:20

7 Answers 7

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]
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.

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
2
  • 1
    The code is correct, but your explanation is not. Jan 4, 2019 at 8:05
  • @MadPhysicist You are right. Let me make some edits
    – Chris
    Jan 4, 2019 at 8:05
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.

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

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]))
3
  • More to the point, it returns None Jan 4, 2019 at 8:06
  • Have made some tabulation in the latest line. Jan 4, 2019 at 8:08
  • I was referring to "just modifies the original". It also returns None. Jan 4, 2019 at 8:14
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]

Not the answer you're looking for? Browse other questions tagged or ask your own question.