The problem is with the loop you are using , Trying modifying it this way

def abs1(array):
    for i in range(len(array)):
        if array[i] < 0:
             array[i] = array[i] * (-1)
    print(array)

The reason is the loop you were previously using was just for accesing list elements but not giving you reference to change anything in list I solved it with using indexes on list.

Answer from KnightKnight on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. ... Return the absolute value of a number. The argument may be an integer, a floating-point number, or an object implementing __abs__().
🌐
Iq-inc
iq-inc.com › python-absolute-value
Python Absolute Value – IQ Inc
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 - Yes, you can find the absolute value of a complex number in Python using the abs() function. This function calculates the magnitude of the complex number as a floating-point value.
🌐
GeeksforGeeks
geeksforgeeks.org › python-absolute-value-of-list-elements
Absolute Value of List Elements | GeeksforGeeks
January 29, 2025 - Output list a contains the positive versions of the numbers from the original list li effectively removing any negative signs. map() function is used to apply the abs() function to each element of list li returning an iterator that produces the absolute values.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - To convert all elements in a list to their absolute values, you can use a list comprehension with abs().
🌐
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....
🌐
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! Want to watch a video instead?
Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › built-in › abs
Python abs()
Become a certified Python programmer. Try Programiz PRO! ... The abs() function returns the absolute value of the given number.
🌐
LearnDataSci
learndatasci.com › solutions › python-absolute-value
Python Absolute Value – abs() for real and complex numbers – LearnDataSci
In regards to Python, real numbers are numbers that are integers or floats. The following example demonstrates how we can apply the abs function to a list of integers:
🌐
w3resource
w3resource.com › python › built-in-function › abs.php
Python: abs() function - w3resource
In Python abs() works with integers and complex numbers. It's return type depends on the type of its argument. >>> type(-2) <class 'int'> >>> type(abs(-2)) <class 'int'> >>> type(-2.0) <class 'float'> >>> type(3+4j) <class 'complex'> >>> type(abs(3+4j)) <class 'float'> >>> Get absolute value of numbers of a list: nums = [12,30,-33,-22,77,-100] print("Original list:") print(nums) print("\nAbsolute values of the above numbers:") print([abs(x) for x in nums]) Output: Original list: [12, 30, -33, -22, 77, -100] Absolute values of the above numbers: [12, 30, 33, 22, 77, 100] Convert a negative number to positive number: n = -452 print(n) print(abs(n)) n = -123.45 print(n) print(abs(n)) Output: -452 452 -123.45 123.45 ·
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
🌐
Server Academy
serveracademy.com › blog › python-absolute-value-abs-tutorial
Python Absolute Value Abs() Tutorial - Server Academy
For example, the absolute value ... normalization. In Python, the simplest way to calculate the absolute value is to use the abs() function....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-maximum-absolute-difference-list-of-list
Python | Maximum absolute difference list of list - GeeksforGeeks
May 5, 2023 - For each pair of corresponding elements from two adjacent lists, we calculate their absolute difference using the abs() function. Keep track of the maximum difference found so far in the variable max_diff.
🌐
TradingCode
tradingcode.net › python › math › absolute-value
How to get absolute value of numbers in Python? • TradingCode
A convenient function to help with that is Python’s enumerate() function. Here’s how that looks: # Some positive and negative values values = [-40, 58, -69, -84, 51, 76, -12, 36] # Loop through the list and replace # each number with its absolute value for index, value in enumerate(values): values[index] = abs(value)
🌐
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, hexadecimal, octal, exponential, etc.
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v1.13 Manual
>>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) 1.5620499351813308
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.1 Manual
>>> import numpy as np >>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) 1.5620499351813308
🌐
GeeksforGeeks
geeksforgeeks.org › abs-in-python
abs() in Python - GeeksforGeeks
August 30, 2024 - Hence we will use the abs() method to calculate the exact time, distance, and speed. ... We declared 3 functions to calculate speed, distance, and time. Then passed the positive and negative integer and float point values to them using the Python abs() function.