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
|
Exit:
[1, -2, -3, 4, -5, 6, 7]
Methods # 2: Using NumPy
It is also possible to use the Python Numpy module which is the most pythonic way to solve this problem. The list is first converted to a numpy array, and then the negative element of the array is returned, which is eventually converted to a list.
|
Exit:
[1, -2, -3, 4, -5 , 6, 7]