You can use the map
and abs
functions to accomplish this:
In [1]: sorted(map(abs, lista))
Out[1]: [1, 2, 3, 5, 7, 8]
To do this with the code you wrote, you can
# The list defined above
lista = [a,b,c,d]
# Sorted from least to greatest absolute value
sorted_abs_list = sorted(map(abs, lista))
# Sorted from greatest to least absolute value
sorted_abs_list = sorted(map(abs, lista), reverse=True)
1
solved How to sort a list and put it in absolute value