SORTING A LIST IN PYTHON

problem

You have a list of items in Python and you wish to sort them, e.g. put them in ascending order. If we assume we have a list of numbers, that would be from the lowest to the greatest (ascending), or vice versa (descending).

SOLUTION

Let’s assume we have the following list of integers.

sorting.py
				
					numbers = [1,90,-22,891,87,44,22,2,4,3,2,1,0]

				
			

In order to sort the list, we can simply use the built-in function sort() as follows:

sorting.py
				
					numbers.sort()

				
			

This will sort the items inside the list and update the list. It will not return a new list but will affect the current list.

So if we now print the list we can see the items are sorted in ascending order.

sorting.py
				
					numbers = [1,90,-22,891,87,44,22,2,4,3,2,1,0]
numbers.sort()
print(numbers)
				
			
output
python sort list
SORTING IN DESCENDING ORDER

Now, if we wish to sort the items of a list in descending order, i.e. from the greatest to the lowest, we can simply do it by passing a boolean parameter to the sort() function as follows:

sorting.py
				
					numbers.sort(reverse=True)
				
			

If we go and print it now:

sorting.py
				
					numbers.sort(reverse=True)
print(numbers)
				
			
output
python sort list descending
reversing items

Now, if we wish to simply reverse the order of the items in the list we can use the reverse() function as follows:

sorting.py
				
					items = ['one','two','three']
items.reverse()
				
			

It will reverse all the items in the list. If we go and print it now:

sorting.py
				
					print(items)
				
			
output
python list.reverse()

conclusion

In this post we saw how simple it is to sort a list of data in Python. We can do both orders ascending and descending order out of the box.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.