Beginner
List Methods, Slicing, and Sorting
📂 Phase 3: Lists, Tuples, Dictionaries, Sets (Days 11–15) · Python ProgrammingBeyond basic add/remove operations, lists support powerful slicing syntax and a rich set of built-in methods for sorting, copying, and transforming data — these are used constantly in real-world Python code and are a favorite area for interview questions.
List Slicing
Slicing extracts a sub-list using the format list[start:stop:step] — exactly the same pattern used for string slicing, with the stop index always exclusive.
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[1:4]) # [20, 30, 40] — indices 1, 2, 3
print(numbers[:3]) # [10, 20, 30] — from the start up to index 3
print(numbers[3:]) # [40, 50, 60] — from index 3 to the end
print(numbers[::2]) # [10, 30, 50] — every second element
print(numbers[::-1]) # [60, 50, 40, 30, 20, 10] — reversed
Sorting a List
| Method | Behavior |
|---|---|
| .sort() | Sorts the list IN PLACE; returns None |
| sorted(list) | Returns a NEW sorted list; the original is unchanged |
numbers = [5, 2, 9, 1, 7]
numbers.sort() # modifies numbers directly
print(numbers) # [1, 2, 5, 7, 9]
original = [5, 2, 9, 1, 7]
new_list = sorted(original) # original stays untouched
print(original) # [5, 2, 9, 1, 7]
print(new_list) # [1, 2, 5, 7, 9]
A very common bug: writing numbers = numbers.sort() — since .sort() returns None, this silently overwrites the list with None. Use .sort() for its side effect, or sorted() if you need to keep the original list intact.
Sorting With a Custom Key
words = ["banana", "kiwi", "apple", "fig"]
words.sort(key=len) # sort by string length
print(words) # ['fig', 'kiwi', 'apple', 'banana']
words.sort(reverse=True) # descending alphabetical order
print(words) # ['kiwi', 'fig', 'banana', 'apple']
Copying a List Correctly
original = [1, 2, 3]
broken_copy = original # NOT a real copy — same object!
broken_copy.append(4)
print(original) # [1, 2, 3, 4] — original was also changed!
real_copy = original.copy() # an actual independent copy
real_copy.append(5)
print(original) # [1, 2, 3, 4] — unaffected
print(real_copy) # [1, 2, 3, 4, 5]
Assigning a list to a new variable name does not copy it — both names point to the exact same list object in memory. Use .copy() or list(original) to create a genuinely independent copy.
List Comprehension — A Concise Way to Build Lists
# Traditional approach
squares = []
for n in range(1, 6):
squares.append(n ** 2)
print(squares) # [1, 4, 9, 16, 25]
# Equivalent list comprehension — one line
squares = [n ** 2 for n in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
List Comprehension With a Condition
numbers = range(1, 11)
evens = [n for n in numbers if n % 2 == 0]
print(evens) # [2, 4, 6, 8, 10]
Other Useful List Methods
| Method | Effect |
|---|---|
| .index(x) | Returns the position of the first matching value |
| .count(x) | Counts how many times a value appears |
| .reverse() | Reverses the list in place |
numbers = [3, 1, 4, 1, 5, 9, 1]
print(numbers.count(1)) # 3 — appears three times
print(numbers.index(4)) # 2 — first occurrence is at index 2
Interview tip: List comprehensions are heavily favored in modern Python code over manual loops with .append() — interviewers frequently ask you to rewrite a basic for loop as a one-line comprehension to test fluency with idiomatic Python.