Sort List in Python sort() function

In this tutorial we will be focusing on how to sort the list in ascending order and descending order in python using sort() Function and sorted() method. We will be also exploring the ways to sort the list based on the length of its value.

  • Sort the python list in Ascending order using sort() method.
  • Sort the python list in Descending order using sort() method.
  • Sort the list in Ascending order using python sorted() method
  • Sort the list in Descending order using python sorted() method
  • Sort the python list by length of its value in ascending order and descending order.
  • Sort the python list by length of its value in ascending order and descending order using sorted() function

 

sort() function to sort the python list in Ascending order:

We will be using sort() function which by default will sort the list in ascending order. List.sort() will sort the list in ascending order as shown below.

### Sort list in Ascending order using sort() function

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits.sort()


print(fruits)

so, the output will be,

output:

[‘Apple’, ‘Apple’, ‘Banana’, ‘Cherry’, ‘Fig’, ‘Kiwi’, ‘Mango’, ‘Papaya’, ‘Pear’, ‘Strawberry’]

 

 

sort() function to sort the python list in Descending order:

We will be using sort() function along with argument reverse=True. which will sort the list in descending order. List.sort(reverse=True) will sort the list in descending order as shown below.

### Sort list in Descending order using sort() function

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits.sort(reverse=True)

print(fruits)

so, the output will be,

output:

[‘Strawberry’, ‘Pear’, ‘Papaya’, ‘Mango’, ‘Kiwi’, ‘Fig’, ‘Cherry’, ‘Banana’, ‘Apple’, ‘Apple’]

 

 

 

sorted() function to sort the python list in Ascending order:

Method 1:

We will be using sorted() function which takes list as an argument by default will sort the list in ascending order. sorted(List) will sort the list in ascending order as shown below.

### Sort list in Ascending order using sorted() function
fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits_sorted=sorted(fruits)
print(fruits_sorted)

so, the output will be,

output:

[‘Apple’, ‘Apple’, ‘Banana’, ‘Cherry’, ‘Fig’, ‘Kiwi’, ‘Mango’, ‘Papaya’, ‘Pear’, ‘Strawberry’]

 

Method 2:

We will be using sorted() function along with lambda, which takes list as an argument, by default will sort the list in ascending order as shown below

### Sort list in Ascending order using lambda

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits_sorted = sorted(fruits, key=lambda x: x)

print(fruits_sorted)

so, the output will be,

output:

[‘Apple’, ‘Apple’, ‘Banana’, ‘Cherry’, ‘Fig’, ‘Kiwi’, ‘Mango’, ‘Papaya’, ‘Pear’, ‘Strawberry’]

 

 

 

sorted() function to sort the python list in Descending order:

Method 1:

We will be using sorted() method which takes list as an argument along with reverse=True will sort the python list in descending order. sorted(List,reverse=True) will sort the list in descending order as shown below.

### Sort list in Descending order using sorted() function


fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits_sorted=sorted(fruits,reverse=True)

print(fruits_sorted)

so, the output will be,

output:

[‘Strawberry’, ‘Pear’, ‘Papaya’, ‘Mango’, ‘Kiwi’, ‘Fig’, ‘Cherry’, ‘Banana’, ‘Apple’, ‘Apple’]

 

Method 2:

We will be using sorted() method and lambda , which takes list as an argument along with reverse=True will sort the python list in descending order as shown below.

### ### Sort list in descending order using lambda


fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits_sorted = sorted(fruits, key=lambda x: x,reverse=True)

print(fruits_sorted)

so, the output will be,

output:

[‘Strawberry’, ‘Pear’, ‘Papaya’, ‘Mango’, ‘Kiwi’, ‘Fig’, ‘Cherry’, ‘Banana’, ‘Apple’, ‘Apple’]

 

Sort vs Sorted Method in python:

Sort-list-in-python-sort-vs-sorted-in-python

 

 

Sort the python list by length of its value in ascending order:

Method 1:

We will be using length function which will be passed as the key to the sort() function. which will sort the python list in ascending order of its element length as shown below.

# Sort the list by length of its value in ascending order
def myFunc(e):
  return len(e)

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits.sort(key=myFunc)
print(fruits)

so, the output will be,

output:

[‘Fig’, ‘Pear’, ‘Kiwi’, ‘Apple’, ‘Mango’, ‘Apple’, ‘Cherry’, ‘Banana’, ‘Papaya’, ‘Strawberry’]

 

Method 2:

We will be using sorted() function which takes list and key=len as argument, which will be sorting the python list in ascending order of its element length as shown below.

### Sort the list by length of its value in ascending order using sorted() function

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits_sorted=sorted(fruits,key=len)

print(fruits_sorted)

so, the output will be,

output:

[‘Fig’, ‘Pear’, ‘Kiwi’, ‘Apple’, ‘Mango’, ‘Apple’, ‘Cherry’, ‘Banana’, ‘Papaya’, ‘Strawberry’]

 

 

 

Sort the python list by length of its value in descending order:

Method 1:

We will be using length method which will be passed as the key to the sort() method along with reverse=True. which will be sorting the python list in descending order of its element length as shown below.

# Sort the list by length of its value in descending order
def myFunc(e):
  return len(e)

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits.sort(reverse=True, key=myFunc)
print(fruits)

so, the output will be,

output:

[‘Strawberry’, ‘Cherry’, ‘Banana’, ‘Papaya’, ‘Apple’, ‘Mango’, ‘Apple’, ‘Pear’, ‘Kiwi’, ‘Fig’]

 

Method 2:

We will be using sorted() function which takes list, key=len  and reverse=True as argument, which will be sorting the python list in ascending order of its element length as shown below.

### Sort the list by length of its value in descending order using sorted() function

fruits = ['Apple', 'Strawberry','Cherry','Mango','Apple','Pear','Kiwi','Banana','Papaya','Fig']

fruits_sorted=sorted(fruits,key=len,reverse=True)


print(fruits_sorted)

so, the output will be,

output:

[‘Strawberry’, ‘Cherry’, ‘Banana’, ‘Papaya’, ‘Apple’, ‘Mango’, ‘Apple’, ‘Pear’, ‘Kiwi’, ‘Fig’]

 

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts