Back
+10 XP
Concept

Sorting Lists: `.sort()` vs `sorted()`

`.sort()` reorganizes the original list directly in place and returns `None`. `sorted(list)` leaves the original alone and hands you a brand new sorted list.

python
nums = [3, 1, 2]
nums.sort()
print(nums)  # [1, 2, 3]
raw = [5, 4]
fresh = sorted(raw)
print(fresh)  # [4, 5]

# 72. Sorting Lists: `.sort()` vs `sorted()` `.sort()` reorganizes the original list directly in place and returns `None`. `sorted(list)` leaves the original untouched and hands you a brand new sorted list: ```python nums = [3, 1, 2] nums.sort() print(nums) # [1, 2, 3] raw = [5, 4] fresh = sorted(raw) print(fresh) # [4, 5] ``` Beginner classic blunder: Typing `nums = nums.sort()`. That assigns the return value (`None`) to `nums`, permanently deleting your list.

Checkpoint

What does `nums.sort()` return?

Practice

Sort `scores = [50, 10, 30]` in place using `.sort()`, then print `scores`.

Terminal
01
Tap code blocks below to assemble
Available Blocks