Skip to ContentGo to accessibility page
Introduction to Python Programming

9.2 Sorting and reversing lists

Introduction to Python Programming9.2 Sorting and reversing lists

9.2 Sorting and reversing lists

Learning objectives

By the end of this section you should be able to

  • Understand the concept of sorting.
  • Use built-in sort() and reverse() methods.

Sorting

Ordering elements in a sequence is often useful. Sorting is the task of arranging elements in a sequence in ascending or descending order.

Sorting can work on numerical or non-numerical data. When ordering text, dictionary order is used. Ex: "bat" comes before "cat" because "b" comes before "c".

Checkpoint

Concepts in Practice

Sorting

1.
What would be the last element of the following list if it is sorted in descending order?
[12, 3, 19, 25, 16, -3, 5]
  1. 25
  2. -3
  3. 5
2.
Arrange the following list in ascending order.
["cat", "bat", "dog", "coyote", "wolf"]
  1. ["bat", "cat", "coyote", "dog", "wolf"]
  2. ["wolf", "coyote", "dog", "cat", "bat"]
3.
How are the words "flask" and "flash" related in Python?
  1. "flask" < "flash"
  2. "flask" == "flash"
  3. "flask" > "flash"

Using
sort()
and
reverse()

Python provides methods for arranging elements in a list.

  • The sort() method arranges the elements of a list in ascending order. For strings, ASCII values are used and uppercase characters come before lowercase characters, leading to unexpected results. Ex: "A" is ordered before "a" in ascending order but so is "G"; thus, "Gail" comes before "apple".
  • The reverse() method reverses the elements in a list.

Example 9.2

Sorting and reversing lists

    # Setup a list of numbers
    num_list = [38, 92, 23, 16]
    print(num_list)

    # Sort the list
    num_list.sort()
    print(num_list)

    # Setup a list of words
    dance_list = ["Stepping", "Ballet", "Salsa", "Kathak", "Hopak", "Flamenco", "Dabke"]

    # Reverse the list
    dance_list.reverse()
    print(dance_list)

    # Sort the list
    dance_list.sort()
    print(dance_list)

The above code's output is:

    [38, 92, 23, 16]
    [16, 23, 38, 92]
    ["Dabke", "Flamenco", "Hopak", "Kathak", "Salsa", "Ballet", "Stepping"]
    ["Ballet", "Dabke", "Flamenco", "Hopak", "Kathak", "Salsa", "Stepping"]

Concepts in Practice

sort() and reverse() methods

Use the following list for the questions below.

board_games = ["go", "chess", "scrabble", "checkers"]
4.
What is the correct way to sort the list board_games in ascending order?
  1. sort(board_games)
  2. board_games.sort()
  3. board_games.sort('ascending')
5.
What is the correct way to reverse the list board_games?
  1. board_games.reverse()
  2. reverse(board_games)
6.
What would be the last element of board_games after the reverse() method has been applied?
  1. 'go'
  2. 'checkers'
  3. 'scrabble'

Try It

Sorting and reversing

Complete the program below to arrange and print the numbers in ascending and descending order.

Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.