Late reply, but the other answer and the linked duplicate question seems to have missed an important aspect of your question, namely that you want the value, not the absolute value, with the largest magnitude. In other words, in a list like [-10, 0, 5], you want the result to be -10, not 10.

To get the value with its original sign, you could (conceptually) follow these steps:

  1. Get a copy of your list where all values were converted to their absolute values
  2. Find the index of the value that has the maximum absolute value
  3. Return the value at that index

This can be done quite easily in numpy by using argmax, which returns the index of the maximum value from an array. Step-by-step, it would look like this:

# Create a numpy array from the list you're searching:
xs = np.array([-10, 0, 5])

# Get an array where each value is converted to its absolute value:
xs_abs = np.abs(xs) 
# This gives [10, 0, 5]

# Get the index of the highest absolute value:
max_index = np.argmax(xs_abs) 
# This gives 0

# Get the number from the original array at the max index:
x = xs[max_index] 
# This gives -10

This can be done in a single line like so:

x = xs[np.argmax(np.abs(xs))]
Answer from Phlippie Bosman on Stack Overflow
🌐
Python.org
discuss.python.org β€Ί python help
A function for calculating magnitude order of a number - Python Help - Discussions on Python.org
September 9, 2022 - What about a function for calculating magnitude order, maybe in math module? Something like this: def magnitude_order(num): if num == 0: return 0 absnum = abs(num) order = math.log10(absnum) res = math.floor(order) return res
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί finding-magnitude-of-a-complex-number-in-python
Finding Magnitude of a Complex Number in Python - GeeksforGeeks
July 23, 2025 - We can manually calculate the magnitude by taking the square root of the sum of the squares of the real and imaginary parts. ... #Manually calculating magnitude def magnitude_manual(z): return (z.real**2 + z.imag**2)**0.5 # Example complex_number ...
🌐
DataCamp
datacamp.com β€Ί tutorial β€Ί python-absolute-value-a-quick-tutorial
Python Absolute Value: A Quick Tutorial | DataCamp
April 11, 2024 - Python offers an easy, built-in function for performing this task, the abs(). The absolute value function allows Python programmers to obtain the magnitude of a number, regardless of its sign, essentially making the number positive.
Top answer
1 of 3
4

Late reply, but the other answer and the linked duplicate question seems to have missed an important aspect of your question, namely that you want the value, not the absolute value, with the largest magnitude. In other words, in a list like [-10, 0, 5], you want the result to be -10, not 10.

To get the value with its original sign, you could (conceptually) follow these steps:

  1. Get a copy of your list where all values were converted to their absolute values
  2. Find the index of the value that has the maximum absolute value
  3. Return the value at that index

This can be done quite easily in numpy by using argmax, which returns the index of the maximum value from an array. Step-by-step, it would look like this:

# Create a numpy array from the list you're searching:
xs = np.array([-10, 0, 5])

# Get an array where each value is converted to its absolute value:
xs_abs = np.abs(xs) 
# This gives [10, 0, 5]

# Get the index of the highest absolute value:
max_index = np.argmax(xs_abs) 
# This gives 0

# Get the number from the original array at the max index:
x = xs[max_index] 
# This gives -10

This can be done in a single line like so:

x = xs[np.argmax(np.abs(xs))]
2 of 3
1

If you mean highest magnitude regardless of negative or positive sign, then you would take the maximum of the list resulting from taking the absolute value of each constituent list value. Does that make sense?

Here is an example:
numbers = [3, 5, 7, -18]

matrix=[]
for x in numbers:
    matrix.append(abs(x))
max(matrix)
🌐
Python.org
discuss.python.org β€Ί python help
A function for calculating magnitude order of a number - Page 2 - Python Help - Discussions on Python.org
September 13, 2022 - What about a function for calculating magnitude order, maybe in math module? Something like this: def magnitude_order(num): if num == 0: return 0 absnum = abs(num) order = math.log10(absnum) re…
🌐
NumPy
numpy.org β€Ί devdocs β€Ί reference β€Ί generated β€Ί numpy.absolute.html
numpy.absolute β€” NumPy v2.5.dev0 Manual
An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2 }\).
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί python-absolute-value-python-abs-tutorial
Python Absolute Value – Python abs Tutorial
June 20, 2022 - You add j to the end of a real number, like so: 1j or 4j.So, an example of a complex number in Python is 2 + 4j or 1 + 2j. Now, when it comes to the return value of the abs() function: For integer numbers, the abs() function returns the absolute ...
🌐
LearnDataSci
learndatasci.com β€Ί solutions β€Ί python-absolute-value
Python Absolute Value – abs() for real and complex numbers – LearnDataSci
The absolute value of a number refers to that value's magnitude, regardless of whether it is positive or negative. For example, the absolute value of -5 is 5. Below is the syntax for using the abs() function to determine the absolute value of a number in Python:
Find elsewhere
🌐
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....
🌐
TutorialsPoint
tutorialspoint.com β€Ί How-to-calculate-absolute-value-in-Python
Python abs() function
May 20, 2025 - Absolute Value of an Integer: 34 Absolute Value of an Float: 154.32 Β· If we pass a complex number as an argument to this function, the return value will be the magnitude of this complex number.
🌐
Educative
educative.io β€Ί answers β€Ί how-to-calculate-an-absolute-value-in-python
How to calculate an absolute value in Python
The abs() function in Python returns the absolute value of the given argument. This argument can be an int, a float, or a complex number. In case of complex numbers, abs() function returns the magnitude part only.
🌐
Javatpoint
javatpoint.com β€Ί python-abs-function
abs() in Python | Python abs() Function with Examples - Javatpoint
abs() in Python | Python abs() Function with Examples on Python, Built in, Functions, abs Function, all Function, bin Function, bool Function, python Functions, new sum Function, bytes Function, new callable Function etc.
🌐
CodeSpeedy
codespeedy.com β€Ί home β€Ί finding magnitude of a complex number in python
Finding Magnitude of a Complex Number in Python - CodeSpeedy
February 18, 2021 - The abs(number) returns : 1. Modulus of the number if it is an Integer or Floating point. 2. Magnitude if the number is Complex.
🌐
Career Karma
careerkarma.com β€Ί blog β€Ί python β€Ί python absolute value: a step-by-step guide
Python Absolute Value: A Step-By-Step Guide | Career Karma
December 1, 2023 - Absolute value is an important ... Absolute values are also often used in data analysis. The Python abs() method calculates the absolute value of a number....
🌐
Linux Hint
linuxhint.com β€Ί python_absolute_value
Python Absolute Value
October 22, 2020 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
SciPy
docs.scipy.org β€Ί doc β€Ί numpy-1.13.0 β€Ί reference β€Ί generated β€Ί numpy.absolute.html
numpy.absolute β€” NumPy v1.13 Manual
Calculate the absolute value element-wise Β· out : ndarray, None, or tuple of ndarray and None, optional
🌐
Delft Stack
delftstack.com β€Ί home β€Ί howto β€Ί numpy β€Ί python numpy magnitude
NumPy Magnitude in Python | Delft Stack
January 30, 2023 - We can also use the numpy.dot() function along with the numpy.sqrt() function to calculate the magnitude of a vector in NumPy. The numpy.dot() function calculates the dot-product between two different vectors, and the numpy.sqrt() function is ...
Top answer
1 of 5
19

Your log10/floor code is perfectly readable, and its performance cost will likely be dwarfed by that of the string formatting you will subsequently be doing on your output.

However, suppose you were to really need the performance...

Note that log10(x) == log2(x) / log2(10) == log2(x) * 1/log2(10)

1/log2(10) is a constant

log2(x) can usually be performed cheaply in the integer pipeline on modern architectures using instructions such as CLZ or a bit twiddling hack, yielding a number between 0 and 63 for a 64-bit integer. That fits in 6 bits, leaving us up to 58 bits after the radix point usable for fixed point arithmetic in a 64-bit type.

So we can then use fixed-point arithmetic to find the log10:

unsigned long long integer_log10( unsigned long long _in )
{
    unsigned long long log10fp6x58 = 0x134413509f79ff0llu; // (unsigned long long) (double(1llu<<58) / log2(10.0))
    return (((integer_log2(_in)) * log10fp6x58)+(1llu<<57)) >> 58;
}

The implementation of integer_log2 is compiler/platform-dependent; e.g. on GCC/PowerPC, it's

unsigned long long integer_log2( unsigned long long _in )
{
    return 63 - __cntlzd(_in);
}

This approach can be generalised for finding the logarithm of any base, simply calculate the appropriate constant as described above.

2 of 5
2

This is the most straightforward and simple method i can think of ... and maybe it will be a bit faster than computing the logarithm:

postfixes = {{1e12, "T"},
             {1e9,  "G"},
             {1e6,  "M"},
             {1e3,  "K"}}

for each postfix in postfixes{
    if(x > postfix.value){
        return (x / postfix.value) + postfix.letter;
    }
}

return x;
🌐
AstroDeck
xspdf.com β€Ί resolution β€Ί 52335468.html
xspdf β€” PDF Generation & Processing API for Developers | xspdf
Support for headers, footers, page numbers, and custom styling. Extract text, images, metadata, and tables from PDF files. Parse complex documents with precision. Convert PDFs to images, Office documents, or convert images and documents to PDF format seamlessly.