How do you arrange a list in ascending order in Python?

Python List sort()

In this tutorial, we will learn about the Python sort() method with the help of examples.

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

Example

prime_numbers = [11, 3, 7, 5, 2]
# sort the list prime_numbers.sort()
print(prime_numbers) # Output: [2, 3, 5, 7, 11]

Python List sort() method

Python list sort() function can be used to sort a List in ascending, descending, or user-defined order.

To Sort the List in Ascending Order

Syntax:

List_name.sort()

This will sort the given list in ascending order. This function can be used to sort a list of integers, floating-point numbers, strings, and others.

Example 1: Sort the List in Ascending Order




numbers = [1, 3, 4, 2]
# Sorting list of Integers in ascending
numbers.sort()
print(numbers)

Output:



[1, 2, 3, 4]

Example 1.1




strs = ["geeks", "code", "ide", "practice"]
# Sorting list of Integers in ascending
strs.sort()
print(strs)

Output:

['code', 'geeks', 'ide', 'practice']

To Sort the List in Descending Order

Syntax:

list_name.sort(reverse=True)

This will sort the given list in descending order.

Example 2: Sort the List in Descending Order




numbers = [1, 3, 4, 2]
# Sorting list of Integers in descending
numbers.sort(reverse = True)
print(numbers)

Output:

[4, 3, 2, 1]

Sorting Using User-defined Order

Syntax:

list_name.sort(key=…, reverse=…) – it sorts according to user’s choice

Parameters:

  • reverse: reverse=True will sort the list descending. Default is reverse=False
  • key: A function to specify the sorting criteria(s)

Example 3: Sorting Using User-defined Order




# Python program to demonstrate sorting by user's
# choice
# function to return the second element of the
# two elements passed as the parameter
def sortSecond(val):
return val[1]
# list1 to demonstrate the use of sorting
# using using second key
list1 = [(1, 2), (3, 3), (1, 1)]
# sorts the array in ascending according to
# second element
list1.sort(key = sortSecond)
print(list1)
# sorts the array in descending according to
# second element
list1.sort(key = sortSecond, reverse = True)
print(list1)

Output:

[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]

How do you arrange a list in ascending order in Python?




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¶

Author

Andrew Dalke and Raymond Hettinger

Release

0.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 Order

This 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() method

To 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 Python

by 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:

  • Implement basic Python sorting and ordering on data structures
  • Differentiate between sorted() and .sort()
  • Customize a complex sort order in your code based on unique requirements

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 Python

You 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.

How do you arrange a list in ascending order in Python?

Sort Python List in Ascending Order

We 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 Online

Output

[14, 21, 28, 31, 65, 74, 84, 96]

Sort Python List in Descending Order

We 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 Online

Output

[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 Online

Output

[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 Online

Output

[21, 28, 14, 96, 84, 65, 74, 31] [96, 84, 74, 65, 31, 28, 21, 14]

Conclusion

In this Python Tutorial, we learned how to sort a list in Python using sort() and sorted() with the help of example programs.