1. You can use abs and map functions like this

     myList = [2,3,-3,-2]
     print(list(map(abs, myList)))
    

Output

    [2, 3, 3, 2]
  1. Or you can use list comprehension like this

     [abs(number) for number in myList]
    
  2. Or you can use list comprehension and a simple if else condition like this

     [-number if number < 0 else number for number in myList]
    
Answer from thefourtheye on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-absolute-value-of-list-elements
Absolute Value of List Elements | GeeksforGeeks
January 29, 2025 - For example, we are having a list li = [-1, 2, -3, 4, -5] we need to convert all the negative values to its absolute values in list so that output should be [1, 2, 3, 4, 5]. List comprehension is used here to apply the abs() function to each ...
๐ŸŒ
Iq-inc
iq-inc.com โ€บ python-absolute-value
Python Absolute Value โ€“ IQ Inc
In Python, to calculate the absolute value of a number you use the abs function. This is a Python built-in function that is always available. abs(0.1) # 0.1 abs(-0.1) # 0.1 abs(-5) # 5 abs(-1e3) # 1000.0 ยท Sometimes though you might need to get the absolute value from every element in a list.
๐ŸŒ
Real Python
realpython.com โ€บ python-absolute-value
How to Find an Absolute Value in Python โ€“ Real Python
June 4, 2025 - To calculate the absolute value of a list of numbers in Python, you can use a list comprehension or the map() function to apply abs() to each element in the list.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - Here's a simple example where abs() always returns 100: class MyClass: def __abs__(self): return 100 mc = MyClass() print(abs(mc)) # 100 ... To convert all elements in a list to their absolute values, you can use a list comprehension with abs().
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python absolute value: abs() in python
Python Absolute Value: Abs() in Python โ€ข datagy
December 15, 2022 - We looped over our list containing our numbers and calculated its absolute value using the abs() function ... Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user!
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_abs.asp
Python abs() Function
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... The abs() function returns the absolute value of the specified number....
Find elsewhere
๐ŸŒ
TradingCode
kodify.net โ€บ python โ€บ math โ€บ absolute-value
How to get absolute value of numbers in Python? โ€ข TradingCode
To use it, we call the function and provide it with an argument that we want the absolute value of. To get the absolute values of a list or array, we have to call abs() on each element.
๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-absolute-value
Python Absolute Value โ€“ abs() for real and complex numbers โ€“ LearnDataSci
As magnitude is just a distance, it will always be positive. We can also use the abs() function on floats. See below for an example of this: real_number_list = [-4.16, -3.12, 11.88, 16.32] for number in real_number_list: absolute_value = abs(number) print(f'Number: {number}, Absolute Value: ...
๐ŸŒ
CodeFatherTech
codefather.tech โ€บ home โ€บ blog โ€บ python absolute value: letโ€™s do some math!
Python Absolute Value: Let's Do Some Math! - CodeFatherTech
December 8, 2024 - Python modules like NumPy and Pandas also allow to calculate the absolute value for more complex data structures. Itโ€™s time for some examples! The simplest way to get the absolute value of a number in Python is with the built-in function abs().
Top answer
1 of 5
5

You can do it like this:

A = [2, 7, 5, 9, 3, 1, 2]

temp = sorted(A)
min_diff = min([abs(i - j) for i, j in zip(temp [:-1], temp [1:])])

print(min_diff)  # -> 0

Sorting makes sure that the element pair (i, j) which produce the overall smallest difference would be a pair of consecutive elements. That makes the number of checks you have to perform much less than the brute force approach of all possible combinations.


Something a bit more clever that short-circuits:

A = [2, 7, 5, 9, 3, 1, 2]


def find_min_diff(my_list):
    if len(set(my_list)) != len(my_list):  # See note 1
        return 0
    else:
        temp = sorted(my_list)
        my_min = float('inf')
        for i, j in zip(temp [:-1], temp [1:]):
            diff = abs(i - j)
            if diff < my_min:
                my_min = diff
        return my_min

print(find_min_diff(A))  # -> 0

Notes:

1: Converting to set removes the duplicates so if the corresponding set has less elements than the original list it means that there is at least one duplicate value. But that necessarily means that the min absolute difference is 0 and we do not have to look any further.

I would be willing to bet that this is the fastest approach for all lists that would return 0.

2 of 5
4

You should not be subtracting 1 from j in the inner loop as you end up skipping the comparison of the last 2. It is better to make the adjustments in the loop ranges, rather than subtracting 1 (or not) in the loop code:

A = [2, 7, 5, 9, 3, 1, 2]

N = 7

mint = 1000

for i in range (0, N-1):
    for j in range (i+1, N):
        if (abs(A[i] - A[j]) < mint):
            mint = abs(A[i] - A[j])
            print(i, j)
            print(mint)
print(mint) # 0

I have also avoided the use of a built-in function name min.


To avoid the arbitrary, magic, number 1000, you can perform an initial check against None:

A = [2, 7, 5, 9, 3, 1, 2]

N = 7

mint = None

for i in range (0, N-1):
    for j in range (i+1, N):
        if mint is None:
            mint = abs(A[i] - A[j])
        elif (abs(A[i] - A[j]) < mint):
            mint = abs(A[i] - A[j])
            print(i, j)
            print(mint)
print(mint) # 0
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ abs
Python abs()
Created with over a decade of experience and thousands of feedback. ... Try Programiz PRO! ... Become a certified Python programmer. Try Programiz PRO! ... The abs() function returns the absolute value of the given number.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Get Absolute Values in Python - YouTube
abs() is a Python built in function that finds the absolute value of a number. abs() can be combined with map() to get the absolute values of all numbers in ...
Published ย  March 26, 2021
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.13.0 โ€บ reference โ€บ generated โ€บ numpy.absolute.html
numpy.absolute โ€” NumPy v1.13 Manual
numpy.absolute(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'absolute'>ยถ
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-absolute-value
Get Absolute Value in Python Without Using abs() Function
October 1, 2025 - While this is not something Iโ€™d recommend for beginners, itโ€™s a clever trick that shows how Python handles numbers under the hood. Another mathematical approach Iโ€™ve used in algorithmic problems is based on the formula: Absolute value of x = โˆš(xยฒ)
๐ŸŒ
GeeksforGeeks
origin.geeksforgeeks.org โ€บ python โ€บ python-absolute-value-of-list-elements
Absolute Value of List Elements - GeeksforGeeks
January 29, 2025 - For example, we are having a list li = [-1, 2, -3, 4, -5] we need to convert all the negative values to its absolute values in list so that output should be [1, 2, 3, 4, 5]. List comprehension is used here to apply the abs() function to each ...
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ references โ€บ methods-and-functions โ€บ python-abs
Python abs() Function: What is Python absolute value and abs() function?
October 13, 2021 - Absolute value of a list is: [43, 5.76, 6.324555320336759, 283] Traceback (most recent call last): File "", line 8, in TypeError: bad operand type for abs(): 'str' Python abs() also works with different formats of numeric values such as binary, ...
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ python-absolute-value
Python Absolute Value
In this article, you learned how to use abs() to find the absolute value of integers, floating points, special numbers, and complex numbers in Python. You also took a look at the fabs() function from the math module, which is a useful alternative for when your output is required to be of the floating point data type. Python comes prepackaged with several functions you can use to perform basic mathematical operations. For instance, you can find the sum or the minimum value in a list of numbers.