Python list sorting(with examples)

Hello Techies,

Python list sorting

In this tutorial, we learn different programs on Python list sorting with the help of multiple examples. The interviewer may ask you about this type of program in Python interviews, so read this blog to the end.

Let’s check all possible programs based on Python list sorting.

Python has two built-in methods for sorting lists and other iterable objects, sort() and sorted().

Python list sorting Programs

Python List sort()

The sort() method sorts the elements of a given list in a specific ascending or descending order. By default, it sorts elements in ascending order.

Syntax of the sort() method:

list.sort(key=..., reverse=...)

sort() Parameters

By default, sort() does not require any additional parameters. However, it has two optional parameters:

  • reverse: Reverse = True value will sort the list in descending order. The default is reverse = false
  • key: Function to specify sorting criteria

Example 1: Python sort list of numbers

# Python list sorting program
# sort list of numbers

list1 = [45, 1, 2, 56, 78, 88]
print("Original list is:", list1)

# sort list of numbers using
# sort() method
list1.sort()
print("Sorted list is:", list1)

Output:

Original list is: [45, 1, 2, 56, 78, 88]
Sorted list is: [1, 2, 45, 56, 78, 88]

Example 2: Sort list in Descending order

# Python list sorting program
# sort list of numbers in descending order

list1 = [45, 1, 2, 56, 78, 88]
print("Original list is:", list1)

# sort list of numbers id=n descending
# order using sort() method

list1.sort(reverse=True)
print("Sorted list is:", list1)

Output:

Original list is: [45, 1, 2, 56, 78, 88]
Sorted list is: [88, 78, 56, 45, 2, 1]

Python List sorted()

The sorted() function sorts the elements in a given order and returns the sorted list. The sorted() function create a new list containing a sorted version of the list.

Syntax of the sorted() method:

sorted(iterable, key=None, reverse=False)

Parameters for the sorted() function

  • iterable: This is the required parameter. This parameter is used for sorting lists, dictionaries, tuples, etc.
  • key: This is an optional parameter. Function to execute to decide the order. None by default.
  • reverse: This is an optional parameter. Reverse = True value will sort the list in descending order. The default is reverse = false.

Example 1: Python sort list of numbers using sorted() function

# Python list sorting program 
# to sort the list of numbers using
# sorted() function

list1 = [45, 1, 2, 56, 78, 88]
print("Original list is:", list1)

# sort list of numbers in ascending
# order using sorted() method

print("Sorted list is:", sorted(list1))

Output:

Original list is: [45, 1, 2, 56, 78, 88]
Sorted list is: [1, 2, 45, 56, 78, 88]

Example 2: Sort list in Descending order using sorted() function

# Python list sorting program 
# to sort list of numbers using
# sorted() function

list1 = [45, 1, 2, 56, 78, 88]
print("Original list is:", list1)

# sort list of numbers in descending
# order using sorted() method

print("Sorted list is:", sorted(list1, reverse=True))

Output:

Original list is: [45, 1, 2, 56, 78, 88]
Sorted list is: [88, 78, 56, 45, 2, 1]
Interview Question:

Q: What is the difference between sort and sorted in python?
Ans: The sort() function will modify the list and The sorted() function will 
           create a new list containing a sorted version of the list.

Python sort list of strings

# Python program to sort a list of strings

lst = ['tech', 'is', 'a', 'life', 'the', 'test']
print("Original List is:", lst)

# Using sort() function
lst.sort()
print("Sorted List is:", lst)

Output:

Original List is: ['tech', 'is', 'a', 'life', 'the', 'test']
Sorted List is: ['a', 'is', 'life', 'tech', 'test', 'the']

Python list sort lambda

In the following program, we are using the lambda expression to sort the list.

# Python program to sort a list

list1 = [[31, 60], [10, 10], [30, 20], [20, 25], [45, 30]]
print("Original List is:", list1)

# Using lambda expression
lst = sorted(list1, key=lambda arg: arg[1])
print("Sorted List is:", lst)

Output:

Original List is: [[31, 60], [10, 10], [30, 20], [20, 25], [45, 30]]
Sorted List is: [[10, 10], [30, 20], [20, 25], [45, 30], [31, 60]]

How to sort a list in Python without sort function

# Python program to sort a list 
# without using the built-in function

list1 = [45, 1, 2, 56, 78, 88]
print("Original List is:", list1)

for i in range(0, list1.__len__()):
    for j in range(1 + i, list1.__len__()):
        if list1[i] > list1[j]:
            temp = list1[i]
            list1[i] = list1[j]
            list1[j] = temp
print("Sorted list is:", list1)

Output:

Original List is: [45, 1, 2, 56, 78, 88]
Sorted list is: [1, 2, 45, 56, 78, 88]

FAQ on Python list sorting

  1. How do I sort list without changing original list?

    You can use the sorted() function to create a new list containing a sorted version of the list.

  2. Can you sort a dictionary python?

    We can sort the dictionary using the sorted() function.

  3. What is the difference between sort and sorted in Python?

    The sort() function will modify the list and The sorted() function will create a new list containing a sorted version of the list.

  4. What does sort () do in Python?

    The sort() method sorts the elements of a given list in a specific ascending or descending order.


Conclusion

In this tutorial, you learned about Python list sorting with the help of multiple examples, We have covered the following points:

  • Python List sort()
  • Python List sorted()
  • Python sort list of strings
  • Python list sort lambda
  • How to sort a list in Python without sort function

Leave a Comment