Advanced List Operations: Sorting, Querying, and Nesting
In this final step, you will explore more advanced list operations, including sorting, querying for information, and working with nested lists.
Let's start with sorting. The sort() method modifies the list in-place. You can sort in ascending or descending order.
Open the file list_operations.py in the WebIDE. Add the following code to demonstrate sorting:
## --- Sorting Lists ---
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print("--- Sorting Lists ---")
print("Original numbers list:", numbers)
## Sort the list in-place (ascending)
numbers.sort()
print("After sort():", numbers)
## Sort the list in descending order
numbers.sort(reverse=True)
print("After sort(reverse=True):", numbers)
## The reverse() method simply reverses the order, it does not sort
letters = ['a', 'b', 'c', 'd']
print("\nOriginal letters list:", letters)
letters.reverse()
print("After reverse():", letters)
Save the file and run it from the terminal:
python ~/project/list_operations.py
The output shows the sorted and reversed lists:
--- Sorting Lists ---
Original numbers list: [3, 1, 4, 1, 5, 9, 2, 6]
After sort(): [1, 1, 2, 3, 4, 5, 6, 9]
After sort(reverse=True): [9, 6, 5, 4, 3, 2, 1, 1]
Original letters list: ['a', 'b', 'c', 'd']
After reverse(): ['d', 'c', 'b', 'a']
Next, let's query a list to find information.
count(): Returns the number of times a value appears.
index(): Returns the index of the first occurrence of a value.
Add the following code to list_operations.py:
## --- Querying Lists ---
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print("\n--- Querying Lists ---")
print("Fruits list:", fruits)
## Count the occurrences of an element
apple_count = fruits.count('apple')
print("Count of 'apple':", apple_count)
## Find the index of the first occurrence of an element
banana_index = fruits.index('banana')
print("Index of first 'banana':", banana_index)
Finally, let's look at nested lists. A nested list is a list that contains other lists as its elements. This is useful for creating 2D structures like a matrix or a grid.
Add this final block of code to list_operations.py:
## --- Nested Lists ---
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("\n--- Nested Lists ---")
print("Matrix:", matrix)
## Access an entire inner list (a row)
first_row = matrix[0]
print("First row:", first_row)
## Access a specific element in the nested list
## To get the element '6', we access row 1, then column 2
element = matrix[1][2]
print("Element at matrix[1][2]:", element)
Save the file and run the script for the last time:
python ~/project/list_operations.py
The complete output will demonstrate sorting, querying, and nested list access:
--- Sorting Lists ---
Original numbers list: [3, 1, 4, 1, 5, 9, 2, 6]
After sort(): [1, 1, 2, 3, 4, 5, 6, 9]
After sort(reverse=True): [9, 6, 5, 4, 3, 2, 1, 1]
Original letters list: ['a', 'b', 'c', 'd']
After reverse(): ['d', 'c', 'b', 'a']
--- Querying Lists ---
Fruits list: ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
Count of 'apple': 2
Index of first 'banana': 3
--- Nested Lists ---
Matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
First row: [1, 2, 3]
Element at matrix[1][2]: 6
You have now mastered several advanced techniques for working with Python lists.