Open In App

Python | Absolute value of list elements

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python list, we need to find the absolute number i.e all the elements with a positive magnitude in list. This problem has an application in various domains of Data Science. Let’s discuss certain ways in which this task can be done.

Method #1: Using list comprehension + abs() This task can be performed using list comprehension technique in which we just iterate each element and keep finding its absolute value using the abs(). 

Python3




# Python3 code to demonstrate working of
# Absolute value of list elements
# using abs() + list comprehension
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements
# using abs() + list comprehension
res = [abs(ele) for ele in test_list]
 
# printing result
print("Absolute value list : " + str(res))


Output

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [5, 6, 7, 8, 10]

Time complexity: O(n), where n is the length of the list “test_list”.
Auxiliary Space: O(n), where n is the length of the list “res”.

Method #2: Using map() + abs() This task can also be performed using map(). As above, the task of map is to perform the task of extending the absolute number logic to each element in list. 

Python3




# Python3 code to demonstrate working of
# Absolute value of list elements
# using map() + abs()
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements
# using map() + abs()
res = list(map(abs, test_list))
 
# printing result
print("Absolute value list : " + str(res))


Output : 

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [5, 6, 7, 8, 10]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #3: Using numpy.absolute()\

Note: Install numpy module using command “pip install numpy”

The numpy library’s absolute() function can be used to find the absolute value of list elements.

Example:

Python3




# Python3 code to demonstrate working of
# Absolute value of list elements
# using numpy.absolute()
 
# importing numpy library
import numpy as np
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements
# using numpy.absolute()
res = np.absolute(test_list)
 
# printing result
print("Absolute value list : " + str(res))


Output:

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [ 5  6  7  8 10]

Time complexity: O(n) as we are iterating through the list once 
Auxiliary space: O(n) as we are creating a new list to store the absolute values of the elements.

Method #4: Using for loops

Python3




# Python3 code to demonstrate working of
# Absolute value of list elements
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements
res=[]
for i in test_list:
    if(i<0):
        res.append(-1*i)
    else:
        res.append(i)
         
# printing result
print("Absolute value list : " + str(res))


Output

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [5, 6, 7, 8, 10]

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #5 : Using  operator.abs() method

Approach

  1. Initiated a for loop to traverse the list
  2. Used operator.abs() to find absolute value of each element of list
  3. Appended the absolute value to output list
  4. Displayed the output list

Python3




# Python3 code to demonstrate working of
# Absolute value of list elements
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements
res=[]
import operator
for i in test_list:
    res.append(operator.abs(i))
# printing result
print("Absolute value list : " + str(res))


Output

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [5, 6, 7, 8, 10]

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #6 : Using math.fabs(): 

Algorithm:

  1. Initialize a list called “test_list”.
  2. Print the original list “test_list”.
  3. Use a list comprehension to iterate over each element in “test_list” and take the absolute value of each element using the “math.fabs()” function.
  4. Convert each element in the resulting list to an integer using “int()”.
  5. Print the resulting list.

Example:

Python3




import math
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements using math.fabs()
res = [int(math.fabs(ele)) for ele in test_list]
 
# printing result
print("Absolute value list : " + str(res))
 
#This code is contributed by Jyothi pinjala.


Output

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [5, 6, 7, 8, 10]

Time complexity: O(n)
The time complexity of this algorithm is O(n) because the list comprehension iterates over each element in the list once, so the time taken to perform the operation is proportional to the number of elements in the list.

Auxiliary Space: O(n)
The space complexity of this algorithm is also O(n) because a new list of the same length as the input list is created to store the absolute values of the input list. Therefore, the space required by the algorithm increases linearly with the size of the input list.

Method #7: Using a lambda function with map()

Use a lambda function with the map() function to find the absolute value of each element in the list. Here are the steps:

  • Define a lambda function that takes an argument and returns its absolute value.
  • Apply the lambda function to each element of the list using the map() function.
  • Convert the map object to a list and assign it to the variable res.
  • Print the result.

Python3




# Python3 code to demonstrate working of
# Absolute value of list elements
# using a lambda function with map()
 
# initialize list
test_list = [5, -6, 7, -8, -10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Absolute value of list elements
# using a lambda function with map()
abs_func = lambda x: abs(x)
res = list(map(abs_func, test_list))
 
# printing result
print("Absolute value list : " + str(res))


Output

The original list is : [5, -6, 7, -8, -10]
Absolute value list : [5, 6, 7, 8, 10]

Time complexity: O(N), where n is the number of elements in the list.
Auxiliary space: O(N), where n is the number of elements in the list (for the res list).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads