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