Bidisha Das
Bidisha Das

Reputation: 177

How to find absolute value of elements in an array?

I have an array containing some negative values. how can i find the absolute value?

for eg suppose my array is

arr = [-2,-5,0,1,2]

and i want an array

arr_out =[2,5,0,1,2]

Upvotes: 0

Views: 2033

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195438

Without numpy using list comprehension:

arr = [-2,-5,0,1,2]
arr_out = [abs(i) for i in arr]

print(arr_out)

Output:

[2, 5, 0, 1, 2]

Upvotes: 1

sjdm
sjdm

Reputation: 589

import numpy as np

arr = [-2,-5,0,1,2] 

print("Absolute value: ", np.absolute(arr))

Upvotes: 0

Related Questions