Python List sort()In this tutorial, we will learn about the Python sort() method with the help of examples. Show
The sort() method sorts the elements of a given list in a specific ascending or descending order. Exampleprime_numbers = [11, 3, 7, 5, 2]# sort the list
prime_numbers.sort()
print(prime_numbers)
# Output: [2, 3, 5, 7, 11]Python List sort() methodPython list sort() function can be used to sort a List in ascending, descending, or user-defined order. To Sort the List in Ascending Order
Example 1: Sort the List in Ascending OrderPython3
Output: [1, 2, 3, 4] Example 1.1Python3
Output: To Sort the List in Descending Order
Example 2: Sort the List in Descending OrderPython3
Output: [4, 3, 2, 1]Sorting Using User-defined Order
Example 3: Sorting Using User-defined OrderPython
Output:
Article Tags :
Python Sorting
python-list python-list-functions Practice Tags :
python-list Sorting What is the sort() method in Python?This method takes a list and sorts it in place. This method does not have a return value. In this example, we have a list of numbers and we can use the sort() method to sort the list in ascending order. my_list = [67, 2, 999, 1, 15] # this prints the unordered list print("Unordered list: ", my_list) # sorts the list in place my_list.sort() # this prints the ordered list print("Ordered list: ", my_list)If the list is already sorted then it will return None in the console. my_list = [6, 7, 8, 9, 10] # this will return None because the list is already sorted print(my_list.sort())The sort() method can take in two optional arguments called key and reverse. key has the value of a function that will be called on each item in the list. In this example, we can use the len() function as the value for the key argument. key=len will tell the computer to sort the list of names by length from smallest to largest. names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"] print("Unsorted: ", names) names.sort(key=len) print("Sorted: ", names)reverse has a boolean value of True or False. In this example, reverse=True will tell the computer to sort the list in reverse alphabetical order. names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"] print("Unsorted: ", names) names.sort(reverse=True) print("Sorted: ", names)Sorting HOW TO¶AuthorAndrew Dalke and Raymond Hettinger Release0.1 Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable. In this document, we explore the various techniques for sorting data using Python. Python Program to Sort List in Ascending OrderThis python programallows a user to enter any integer value, and we considerit is a length of a List. Next, we used For Loop to add numbers to the Python list. Python sort function sort the List items in Ascending Order. # Python Program to Sort List in Ascending Order NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) NumList.sort() print("Element After Sorting List in Ascending Order is : ", NumList)Sorting Python List in ascending order output Please enter the Total Number of List Elements: 4 Please enter the Value of 1 Element : 56 Please enter the Value of 2 Element : 76 Please enter the Value of 3 Element : 44 Please enter the Value of 4 Element : 2 Element After Sorting List in Ascending Order is : [2, 44, 56, 76]Introduction to the Python List sort() methodTo sort a list, you use the sort() method: list.sort() Code language: Python (python)The sort() method sorts the original list in place. It means that the sort() method modifies the order of elements in the list. By default, the sort() method sorts the elements of a list using the less-than operator (<). In other words, it places the lower elements before the higher ones. To sort elements from higher to lower, you pass the reverse=True argument to the sort() method like this: list.sort(reverse=True) Code language: Python (python) How to Use sorted() and sort() in Pythonby David Fundakowski basics python Mark as Completed Tweet Share Email Table of Contents Remove ads Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Sorting Data With Python All programmers will have to write code to sort items or data at some point. Sorting can be critical to the user experience in your application, whether it’s ordering a user’s most recent activity by timestamp, or putting a list of email recipients in alphabetical order by last name. Python sorting functionality offers robust features to do basic sorting or customize ordering at a granular level. In this guide, you’ll learn how to sort various types of data in different data structures, customize the order, and work with two different methods of sorting in Python. By the end of this tutorial, you’ll know how to:
For this tutorial, you’ll need a basic understanding of lists and tuples as well as sets. Those data structures will be used in this tutorial, and some basic operations will be performed on them. Also, this tutorial uses Python 3, so example output in this tutorial might vary slightly if you’re using Python 2. Free Bonus: Click here to get access to a chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code. Sort List in PythonYou can sort a list in ascending or descending order using list.sort() or sorted(list). sort() can accept a boolean argument called reverse which by default is False and sorts the list in ascending order. If this parameter is given True, the list shall be sorted in descending order. Sort Python List in Ascending OrderWe shall not provide any argument to sort(), hence the default operation of sorting the list in ascending order should happen. example.py – Python Program #initialize list aList = [21, 28, 14, 96, 84, 65, 74, 31] #sort list aList.sort() #print sorted list print(aList)Try OnlineOutput [14, 21, 28, 31, 65, 74, 84, 96]Sort Python List in Descending OrderWe shall provide the value of reverse parameter of sort() as True. Then the sorting will happen in Descending order. example.py – Python Program #initialize list aList = [21, 28, 14, 96, 84, 65, 74, 31] #sort list aList.sort(reverse=True) #print sorted list print(aList)Try OnlineOutput [96, 84, 74, 65, 31, 28, 21, 14]Little more about sort() and introduction to sorted()sort() does perform in-place sorting. So, the original list is modified. If you would like to keep the original list unchanged, you can use sorted(). sorted() returns the sorted list with doing any modifications to the original list. example.py – Python Program #initialize list aList = [21, 28, 14, 96, 84, 65, 74, 31] #sort list sortedList = sorted(aList) #print list print(aList) #print sorted list print(sortedList)Try OnlineOutput [21, 28, 14, 96, 84, 65, 74, 31] [14, 21, 28, 31, 65, 74, 84, 96]You can use the same parameter as we used with sort() to sort a list in descending order. example.py – Python Program #initialize list aList = [21, 28, 14, 96, 84, 65, 74, 31] #sort list sortedList = sorted(aList, reverse=True) #print list print(aList) #print sorted list print(sortedList)Try OnlineOutput [21, 28, 14, 96, 84, 65, 74, 31] [96, 84, 74, 65, 31, 28, 21, 14]ConclusionIn this Python Tutorial, we learned how to sort a list in Python using sort() and sorted() with the help of example programs. |