Question
Difference between sort() and sorted()?
Solution
✔ Verified
1. sort()
- Method used only on lists
- Sorts the list in place (modifies the original list)
- Does not return the sorted list (returns None)
- More memory efficient because it doesn’t create a new list
numbers = [3, 1, 4, 2] numbers.sort() print(numbers) # Output: [1, 2, 3, 4]
2. sorted()
- Built-in function that works on any iterable (like lists, tuples, strings, sets, etc.)
- Returns a new sorted list
- Does not change the original data
numbers = [3, 1, 4, 2] new_list = sorted(numbers) print(numbers) # Output: [3, 1, 4, 2] print(new_list) # Output: [1, 2, 3, 4]
Click here to download practice questions on
Python Lists