Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo
Introduction to Python Programming

9.2 Sorting and reversing lists

Introduction to Python Programming9.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

Sorting

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

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

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo 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.