Sorting Python Lists

A cheerful goose sorts colourful books by size on a shelf, with bold text “Sorting Python Lists” against a pale magenta background.

Sorting is a common task in programming. Whether you want to put numbers in order or tidy up a list of names, Python gives you simple tools to do it. Let’s look at how it works.

Using sort()

The sort() method arranges the items in a list into order. By default, it sorts in ascending order — that means from smallest to largest for numbers, and alphabetically for text.

This changes the original list. The items are now permanently in order.

Things to try:

  • Create a list of five numbers in random order and sort them.
  • Make a list of names and use sort() to arrange them alphabetically.

Sorting in Reverse

If you’d like to sort a list in the opposite direction (descending order), you can use the reverse=True option inside sort().

This is useful when you want the biggest numbers or the last letters first.

Things to try:

  • Sort a list of numbers in descending order.
  • Sort a list of words so they appear from Z to A.

Using sorted()

The sorted() function looks very similar to sort(), but there’s one key difference: it does not change the original list. Instead, it creates a brand-new sorted version while leaving the original untouched.

This is handy when you want both the original and the sorted list available. Like sort(), you can also add reverse=True to flip the order.

Things to try:

  • Use sorted() on a list of numbers, then check that the original list hasn’t changed.
  • Try sorting the same list both forwards and backwards using sorted().

Wrapping Up

The main difference to remember is that sort() changes your list directly, while sorted() gives you a new one. Both are useful, depending on whether you want to keep the original order or not. Once you’re comfortable with these tools, you’ll find sorting lists becomes second nature.

Main Topic

Introducing Python Lists

A cartoon frog smiles while writing “Python Lists” with items flies, worms, insects on a chalkboard in a classroom setting.

This is a Python series introducing lists, their importance, and everyday uses. Light-hearted explanations make coding concepts simple, clear, and fun.

Other Tutorials in this Topic

A person’s hands holding pen and eraser above a notepad on a desk, crossing out “bread” from a shopping list.

Beginning Python Lists

A practical Python tutorial introducing lists, showing how to access, add, modify, and remove items with clear…

A smiling blacksmith hammers words into metal blocks on an anvil, labelled “cat,” “7,” and “apple,” symbolising immutable Python tuples.

Tuples in Python

A Python tutorial explaining tuples: how to create them, their immutability, practical uses, adding to lists, and…

A person in an orange shirt works along a conveyor belt with boxes, apple, and envelope under text “Iterating Lists” on pale magenta.

Iterating Lists in python

A clear Python tutorial explaining how to loop through lists with for, while, and enumerate, introducing iterators…

A cheerful cartoon monkey sorts bananas from a mixed fruit box into another box on a conveyor belt, with “Filtering Lists” text.

Maps and Filters in python

This tutorial explains how to use Python’s `map()` and `filter()` functions to transform and select list items,…