Python Question

How to sort a list and put it in absolute value

I have this list that contains positive and negative elements but now I need to sort the list and put it in absolute value, for example:

list[-2,-3,8,-5,1,7]===>list[1,2,3,5,7,8]
Is it clear?
The code:

a=5
b=6
c=-3
d=-8

lista = [a,b,c,d]
lista.sort()
lista.reverse()

Answer

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)