Python | Sort a list of percentage

• Last Updated : 11 May, 2020

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.

 `# 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``)`

Output:

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

Code #2:

 `# 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)`

Output:

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

My Personal Notes arrow_drop_up