How to Sort a List in Python
In this tutorial, you'll learn how to sort a list using the list sort() method in Python. Also, this tutorial illustrates sample programs on how to use the range() function and the FOR loop statement to sort a list of numbers, strings, and lists without using the list sort() method.
List sort() Method Syntax
To sort a list in Python, you use the following syntax:
list.sort(key=None, reverse=False )
Remarks
- list: The list that you want to sort
- The reverse argument defaults to False if omitted. If True, the list sorted in descending order.
- The key argument defaults to None if omitted.
Examples
Sort a List of Numbers
The following code illustrates an example of how to sort a list of numbers:
# sort a list of numbers in ascending ordernumbers = [200, 100, 400, 500, 300, 400, 700, 800, 600]numbers.sort()print(numbers)
Output:
[100, 200, 300, 400, 400, 500, 600, 700, 800]
Sort a List of Strings Alphabetically
The following program illustrates how to sort a list of strings alphabetically (letters and number):
# sort a list of strings alphabetically (letters and number)brands = ["Microsoft", "Oracle", "Facebook", "Twitter", "1G", "2M","Apple", "Google", "Amazon", "Ebay", "Alibaba", "Yahoo"]brands.sort()print(brands)
Output:
['1G', '2M', 'Alibaba', 'Amazon', 'Apple', 'Ebay', 'Facebook', 'Google', 'Microsoft', 'Oracle', 'Twitter', 'Yahoo']
Sort a List of Strings Alphabetically, in Descending Order
The following is an example of how to sort a list of strings alphabetically, in descending order:
# sort a list of strings alphabetically in descending orderbrands = ["Microsoft", "Oracle", "Facebook", "Twitter", "1G", "2M","Apple", "Google", "Amazon", "Ebay", "Alibaba", "Yahoo"]brands.sort(reverse=True)print(brands)
Output:
['Yahoo', 'Twitter', 'Oracle', 'Microsoft', 'Google', 'Facebook', 'Ebay', 'Apple', 'Amazon', 'Alibaba', '2M', '1G']
Sort a List of Strings by Length
The following is an example of how to sort a list of strings by length, in ascending order:
# sort a list of strings by length in ascending orderbrands = ["Microsoft", "Oracle", "Facebook", "Twitter", "1G", "2M","Apple", "Google", "Amazon", "Ebay", "Alibaba", "Yahoo"]brands.sort(key=len)print(brands)
Output:
['1G', '2M', 'Ebay', 'Apple', 'Yahoo', 'Oracle', 'Google', 'Amazon', 'Twitter', 'Alibaba', 'Facebook', 'Microsoft']
Sort a List of Strings by Length, in Descending Order
# sort a list of strings by length in descending orderbrands = ["Microsoft", "Oracle", "Facebook", "Twitter", "1G", "2M","Apple", "Google", "Amazon", "Ebay", "Alibaba", "Yahoo"]brands.sort(key=len, reverse=True)print(brands)
Output:
['Microsoft', 'Facebook', 'Twitter', 'Alibaba', 'Oracle', 'Google', 'Amazon', 'Apple', 'Yahoo', 'Ebay', '1G', '2M']
Sort a List of Lists
The following example illustrates how to sort a list of lists, by the first element of the sublists, in ascending order:
# sort a list of lists by the first element of the sublist, in ascending orderstudent_list1 = ["Addison", "Jonh", "Roland", "Anthony", "Olivia", "Elizabeth"]student_list2 = ["Jackson", "Olivia", "Jmaes", "Emma", "Christopher"]student_list3 = ["Andrew", "Olivia", "James", "Brooklyn"]student_list = [student_list1, student_list2, student_list3]student_list.sort()print(student_list)
Output:
[['Addison', 'Jonh', 'Roland', 'Anthony', 'Olivia', 'Elizabeth'], ['Andrew', 'Olivia', 'James', 'Brooklyn'], ['Jackson', 'Olivia', 'Jmaes', 'Emma', 'Christopher']]
Sort a List of Lists by Length
The following code is an example of how to sort a list of lists by length, in ascending order:
# sort a list of lists by length, in ascending orderstudent_list1 = ["Addison", "Jonh", "Roland", "Anthony", "Olivia", "Elizabeth"]student_list2 = ["Jackson", "Olivia", "Jmaes", "Emma", "Christopher"]student_list3 = ["Andrew", "Olivia", "James", "Brooklyn"]student_list = [student_list1, student_list2, student_list3]student_list.sort(key=len)print(student_list)
Output:
[['Andrew', 'Olivia', 'James', 'Brooklyn'], ['Jackson', 'Olivia', 'Jmaes', 'Emma', 'Christopher'], ['Addison', 'Jonh', 'Roland', 'Anthony', 'Olivia', 'Elizabeth']]
Sort a List of Lists by Length, in Descending Order
The following code is an example of how to sort a list of lists by length, in descending order:
# sort a list of lists by length, in descending orderstudent_list1 = ["Addison", "Jonh", "Roland", "Anthony", "Olivia", "Elizabeth"]student_list2 = ["Jackson", "Olivia", "Jmaes", "Emma", "Christopher"]student_list3 = ["Andrew", "Olivia", "James", "Brooklyn"]student_list = [student_list1, student_list2, student_list3]student_list.sort(key=len, reverse=True)print(student_list)
Output:
[['Addison', 'Jonh', 'Roland', 'Anthony', 'Olivia', 'Elizabeth'], ['Jackson', 'Olivia', 'Jmaes', 'Emma', 'Christopher'], ['Andrew', 'Olivia', 'James', 'Brooklyn']]
Sort a List of Numbers without Using sort()
The following illustrates an example of how to write a program to sort a list of numbers in ascending order:
# how to sort numbers in python without the sort() function or method# sort a list of numbers, in ascending ordernumbers = []numbers = [200, 100, 400, 500, 300, 400, 700, 800, 600]length = len(numbers)for i in range(length):for j in range(i + 1, length):if numbers[i] > numbers[j]:num1 = numbers[i]num2 = numbers[j]numbers[i] = num2numbers[j] = num1print(numbers)
Output:
[100, 200, 300, 400, 400, 500, 600, 700, 800]
The following illustrates an example of how to write a program to sort a list of numbers in descending order:
# how to sort numbers in python without the sort() function or method# sort a list of numbers, in descending ordernumbers = []numbers = [200, 100, 400, 500, 300, 400, 700, 800, 600]length = len(numbers)for i in range(length):for j in range(i + 1, length):if numbers[i] < numbers[j]:num1 = numbers[i]num2 = numbers[j]numbers[i] = num2numbers[j] = num1print(numbers)
Output:
[800, 700, 600, 500, 400, 400, 300, 200, 100]
The following program sorts a list of strings in ascending order:
# how to sort a list alphabetically in python without the sort() function or method# sort a list of fruits, in ascending orderfruits = ["Mango", "Apple", "Kiwi", "Orange", "Grape", "Orange", "Grapefruit"]length = len(fruits)for i in range(length):for j in range(i + 1, length):if fruits[i] > fruits[j]:f1 = fruits[i]f2 = fruits[j]fruits[i] = f2fruits[j] = f1print(fruits)
Output:
['Apple', 'Grape', 'Grapefruit', 'Kiwi', 'Mango', 'Orange', 'Orange']
Here, the range() function and the FOR loop statement used to sort a list.
In this tutorial, you've learned how to use the list sort() method in Python. Also, this tutorial illustrated sample programs on how to sort a list without using the list sort() method.