Concept Flow - Sorting and reversing lists
Start with list
Choose operation
Sort list
Sorted list
End
We start with a list, then choose to either sort it or reverse it, resulting in a new list order.
Jump into concepts and practice - no test required
numbers = [4, 2, 9, 1] numbers.sort() print(numbers) numbers.reverse() print(numbers)
| Step | Operation | List Before | Action | List After | Output |
|---|---|---|---|---|---|
| 1 | Start | [4, 2, 9, 1] | None | [4, 2, 9, 1] | |
| 2 | sort() | [4, 2, 9, 1] | Sort ascending | [1, 2, 4, 9] | |
| 3 | print() | [1, 2, 4, 9] | Print list | [1, 2, 4, 9] | [1, 2, 4, 9] |
| 4 | reverse() | [1, 2, 4, 9] | Reverse order | [9, 4, 2, 1] | |
| 5 | print() | [9, 4, 2, 1] | Print list | [9, 4, 2, 1] | [9, 4, 2, 1] |
| 6 | End | [9, 4, 2, 1] | None | [9, 4, 2, 1] |
| Variable | Start | After sort() | After reverse() | Final |
|---|---|---|---|---|
| numbers | [4, 2, 9, 1] | [1, 2, 4, 9] | [9, 4, 2, 1] | [9, 4, 2, 1] |
Sorting and reversing lists in Python: - Use list.sort() to sort the list in place (changes original). - Use list.reverse() to reverse the list order in place. - Use sorted(list) to get a new sorted list without changing original. - Both sort() and reverse() do not return a new list. - Print to see the current list state after operations.
list.sort() method do to a list in Python?list.sort()list.sort() method changes the original list to arrange its items in ascending order.list.reverse(), which flips the list order, list.sort() orders items from smallest to largest.numbers in descending order?sort() method accepts reverse=True to sort in descending order.numbers.sort(reverse=True). Other options use invalid parameters or method calls.items = [3, 1, 4, 2] items.sort() items.reverse() print(items)
items.sort()items.reverse()data in descending order. What is wrong?data = [5, 2, 9, 1] data.reverse() data.sort() print(data)
words = ['apple', 'banana', 'cherry', 'date']. You want to sort them in reverse alphabetical order without changing the original list. Which code snippet achieves this?sort() and sorted()sort() changes the original list; sorted() returns a new sorted list.sorted(words, reverse=True) returns a new list sorted in reverse alphabetical order, leaving words unchanged.