Open In App

Sort a List According to the Length of the Elements – Python

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list of strings and our task is to sort the list based on the length of each string. For example, if the input is [“Python”, “C”, “Java”, “JavaScript”, “Go”], the output should be [“C”, “Go”, “Java”, “Python”, “JavaScript”] since shorter strings come first.

Using sorted()

sorted() function allows us to sort the list by specifying the key parameter, using key = len we can sort the elements based on their length.

Python
a = ["Python", "C", "Java", "JavaScript", "Go"]

# Sorting using sorted() with key=len
b = sorted(a, key=len)

print(b)

Output
['C', 'Go', 'Java', 'Python', 'JavaScript']

Explanation:

  • key = len argument ensures sorting is based on the length of each element.
  • sorted() function creates a new list without modifying the original.

Let’s explore some more ways and see how we can sort a list according to the length of the elements.

Using sort()

sort() method sorts the list in place, modifying the original list instead of creating a new one.

Python
a = ["Python", "C", "Java", "JavaScript", "Go"]

# Sorting using sort() with key=len
a.sort(key=len)

print(a)

Output
['C', 'Go', 'Java', 'Python', 'JavaScript']

Explanation:

  • key = len argument works similarly to the sorted() function.
  • sort() method directly changes the original list.
  • We can use this method if we do not need to retain the original order of the list.

Using Custom Function as the Sorting Key

A custom function can provide additional flexibility in sorting logic.

Python
a = ["Python", "C", "Java", "JavaScript", "Go"]

# Custom function for sorting
def len_sort(s):
    return len(s)

# Sorting using custom function
b = sorted(a, key=len_sort)

print(b)

Output
['C', 'Go', 'Java', 'Python', 'JavaScript']

Explanation:

  • custom function len_sort() calculates the length of each element.
  • sorted() function uses this custom function to determine the sorting order.

Sorting by Length in Descending Order

If we want to sort the list in descending order of element length, we can add the reverse=True parameter to the sorted() function.

Python
a = ["Python", "C", "Java", "JavaScript", "Go"]

# Sorting in descending order of length
b = sorted(a, key=len, reverse=True)

print(b)

Output
['JavaScript', 'Python', 'Java', 'Go', 'C']

Explanation:

  • key=len argument sorts the list by length and reverse=True argument ensures the order is descending.
  • This is useful when the longest elements need to appear first.

Using List Comprehension with sorted()

List comprehension can be combined with sorted() to create a new sorted list while keeping the original list intact.

Python
a = ["Python", "C", "Java", "JavaScript", "Go"]

# Sorting using list comprehension
b = [x for x in sorted(a, key=len)]

print(b)

Output
['C', 'Go', 'Java', 'Python', 'JavaScript']

Explanation:

  • sorted() function sorts the list based on the length of elements.
  • List comprehension creates a new list from the sorted result.


Next Article

Similar Reads