
Python
Learning Level
Understand the algorithms behind list operations and how to optimize for performance.
List methods modify the original list (in-place), returning `None`. Understanding this prevents bugs.
# In-place: sort() returns None, modifies original
lst = [3, 1, 4, 1, 5, 9]
result = lst.sort()
print(result) # None
print(lst) # [1, 1, 3, 4, 5, 9]
# Creates copy: sorted() returns new list
lst = [3, 1, 4, 1, 5, 9]
sorted_lst = sorted(lst)
print(lst) # [3, 1, 4, 1, 5, 9] - unchanged
print(sorted_lst) # [1, 1, 3, 4, 5, 9]
# Common mistake: assuming reverse() returns list
original = [3, 2, 1]
reversed_lst = original.reverse()
print(reversed_lst) # None (trap!)
print(original) # [1, 2, 3] - original is reversed
# Correct approach
reversed_lst = original[::-1] # Creates new listPython uses Timsort, a hybrid algorithm combining merge sort and insertion sort, optimized for real-world data.
# Timsort performance characteristics
import time
import random
# Best case: already sorted O(n)
sorted_list = list(range(100000))
start = time.time()
sorted(sorted_list)
print(f"Already sorted: {time.time() - start:.4f}s") # Very fast
# Worst case: reverse sorted O(n log n)
reverse_sorted = list(range(100000, 0, -1))
start = time.time()
sorted(reverse_sorted)
print(f"Reverse sorted: {time.time() - start:.4f}s") # Slower
# Random: O(n log n)
random_list = random.sample(range(100000), 100000)
start = time.time()
sorted(random_list)
print(f"Random: {time.time() - start:.4f}s")The `key` parameter allows custom sorting without explicit comparison.
# Sort by computed value
students = [
{"name": "Alice", "grade": 92},
{"name": "Bob", "grade": 78},
{"name": "Carol", "grade": 85}
]
# Sort by grade (descending)
sorted_by_grade = sorted(students, key=lambda s: s["grade"], reverse=True)
# [Alice(92), Carol(85), Bob(78)]
# Sort strings by length
words = ["python", "data", "structures"]
by_length = sorted(words, key=len)
# ['data', 'python', 'structures']
# Complex key function
tuples = [(2, 3), (1, 5), (1, 2)]
by_sum = sorted(tuples, key=lambda t: t[0] + t[1])
# [(1, 2), (2, 3), (1, 5)] - sorted by sum
# Stable sort: preserves original order for equal keys
data = [(1, 'a'), (2, 'b'), (1, 'c'), (2, 'd')]
by_first = sorted(data, key=lambda x: x[0])
# [(1, 'a'), (1, 'c'), (2, 'b'), (2, 'd')] - preserves order of 1s and 2s| Method | Complexity | In-place | Notes |
|---|---|---|---|
| `sort()` | O(n log n) | Yes | Returns None |
| `sorted()` | O(n log n) | No | Returns new list |
| `reverse()` | O(n) | Yes | Returns None |
| `insert(i, x)` | O(n) | Yes | Shifts elements |
| `remove(x)` | O(n) | Yes | Removes first match |
| `pop(i)` | O(n) | Yes | O(1) if i == -1 |
| `clear()` | O(n) | Yes | Deallocates memory |
| `extend(x)` | O(k) | Yes | k = len(x) |
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep β’ Build Real β’ Verify Skills β’ Launch Forward