Python | Sort a list of percentage

Given a list of percentage, write a Python program to sort the given list in ascending order.

Let’s see different ways to do the task.

Code #1: Chops ‘%’ in string and convert it into float.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to sort list of percentage 
  
# List initialization
Input =['2.5 %', '6.4 %', '91.6 %', '11.5 %']
  
# removing % and converting to float
# then apply sort function
Input.sort(key = lambda x: float(x[:-1]))
  
# printing output
print(Input)

chevron_right


Output:

['2.5 %', '6.4 %', '11.5 %', '91.6 %']

 
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to sort list of percentage 
  
# List initialization
Input =['2.5 %', '6.4 %', '91.6 %', '11.5 %']
  
# Temporary list initialization
temp = []
  
# removing % sign
for key in Input:
    temp.append((key[:-1]))
  
# sorting list of float
temp = sorted(temp, key = float)
  
# Output list initialization
output = []
  
# Adding percentage sign
for key in temp:
    output.append(key + '%')
  
# printing output
print(output)

chevron_right


Output:

['2.5 %', '6.4 %', '11.5 %', '91.6 %']



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.