9.2: Sorting and Reversing Lists
- Page ID
- 117579
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
By the end of this section you should be able to
- Understand the concept of sorting.
- Use built-in
sort()andreverse()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".
What would be the last element of the following list if it is sorted in descending order?
[12, 3, 19, 25, 16, -3, 5]
25-35
- Answer
-
b. Sorting in descending order means to arrange the elements from largest to smallest. -3 is the smallest element of the list and would be the last element when the list is sorted in descending order.
Arrange the following list in ascending order.
["cat", "bat", "dog", "coyote", "wolf"]
["bat", "cat", "coyote", "dog", "wolf"]["wolf", "coyote", "dog", "cat", "bat"]
- Answer
-
a. For strings, ascending order refers to dictionary order.
How are the words "flask" and "flash" related in Python?
"flask" < "flash""flask" == "flash""flask" > "flash"
- Answer
-
c. In alphabetical order, "h" comes before "k". The first four characters are the same, so
"flash"comes before"flask".
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.
# 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"]
Use the following list for the questions below.
board_games = ["go", "chess", "scrabble", "checkers"]
What is the correct way to sort the list board_games in ascending order?
sort(board_games)board_games.sort()board_games.sort('ascending')
- Answer
-
b. The
sort()function must be applied on the list. The default for thesort()function is to arrange elements in ascending order.
What is the correct way to reverse the list board_games?
board_games.reverse()reverse(board_games)
- Answer
-
a. The correct syntax for the
reverse()function requires the function to be applied to the list.
What would be the last element of board_games after the reverse() method has been applied?
'go''checkers''scrabble'
- Answer
-
a.
"go"is the first element ofboard_games, so"go"would be the last element onceboard_gamesis reversed.
Complete the program below to arrange and print the numbers in ascending and descending order.
Interactive Code
- Answer
-
num_list = [3, 32, -23, 699, 75, -101, 22, 45, 94, -3, 2]# To sort in ascending order use list sort() function
num_list.sort()
print(num_list)# Sorting in descending order is simply reversing the already sorted list
num_list.reverse()
print(num_list)


