# Python | Reverse sign of each element in given list

• Last Updated : 23 Jan, 2023

Given a list of integers, write a Python program to reverse the sign of each element in given list. Examples:

```Input : [-1, 2, 3, -4, 5, -6, -7]
Output : [1, -2, -3, 4, -5, 6, 7]

Input : [-5, 9, -23, -2, 7]
Output : [5, -9, 23, 2, -7]```

Methods #1: List comprehension

## Python3

 `# Python3 program to Convert positive``# list integers to negative and vice-versa``def` `Convert(lst):``    ``return` `[ ``-``i ``for` `i ``in` `lst ]` `# Driver code``lst ``=` `[``-``1``, ``2``, ``3``, ``-``4``, ``5``, ``-``6``, ``-``7``]``print``(Convert(lst))`

Output:

`[1, -2, -3, 4, -5, 6, 7]`

Time Complexity: O(n).

Space Complexity: O(1).

Methods #2 : Using numpy

The Python module, Numpy, can also be used which is the most pythonic way to solve the given problem. The list is first converted to numpy array and then, the negative of the array is returned, which is finally converted to list.

## Python3

 `# Python3 program to Convert positive``# list integers to negative and vice-versa``import` `numpy as np`  `def` `Convert(lst):``    ``lst ``=` `np.array(lst)``    ``return` `list``(``-``lst)`  `# Driver code``lst ``=` `[``-``1``, ``2``, ``3``, ``-``4``, ``5``, ``-``6``, ``-``7``]``print``(Convert(lst))`

Output:

`[1, -2, -3, 4, -5, 6, 7]`

Time Complexity: O(n).

Space Complexity: O(1).

Method #3 : Using startswith() and replace() methods

## Python3

 `# Python3 program to Convert positive``# list integers to negative and vice-versa` `lst ``=` `[``-``1``, ``2``, ``3``, ``-``4``, ``5``, ``-``6``, ``-``7``]``nl ``=` `list``(``map``(``str``, lst))``res ``=` `[]``for` `i ``in` `nl:``    ``if``(i.startswith(``"-"``)):``        ``i ``=` `i.replace(``"-"``, "")``        ``res.append(``int``(i))``    ``else``:``        ``i ``=` `"-"``+``i``        ``res.append(``int``(i))``print``(res)`

Output

`[1, -2, -3, 4, -5, 6, 7]`

Time Complexity: O(n).

Space Complexity: O(n).

Method #4: Using map function and neg operator methods

## Python

 `# Python3 program to Convert positive``# list integers to negative and vice-versa``from` `operator ``import` `neg``lst ``=` `[``-``1``, ``2``, ``3``, ``-``4``, ``5``, ``-``6``, ``-``7``]``nl ``=` `list``(``map``(neg, lst))``print``(nl)`

Output:

`[1, -2, -3, 4, -5, 6, 7]`

Time Complexity: O(n).

Space Complexity: O(1).

My Personal Notes arrow_drop_up