Lesson 04
List Methods — sort(), reverse(), copy(), and del
Sort and reverse Python lists in place, copy safely with copy() vs aliasing, and delete elements with the del statement.
What you will learn
Organize · Duplicate · Delete Lists
This lesson focuses on organizing data, creating safe copies, and deleting elements. These methods are essential for interviews because they test your understanding of memory (id()), mutation, and shallow copies.
| Tool | Role |
|---|---|
copy() |
Independent list object |
sort() |
Arrange values in order |
reverse() |
Flip existing order in place |
del |
Delete item, slice, or variable name |
Unlike File 02 — mutation basics and File 03 — search and merge, this lesson emphasizes memory behavior and in-place vs new object.
Learning roadmap
Memory formula: Copy → Sort → Reverse → Delete
1. copy() — create an independent list
The biggest beginner mistake is confusing copy with alias.
Alias vs copy (visual)
a = [10, "RS"]
b = a.copy()
print(a is b) # FalseAfter modifying one list:
a.append("Python")
b.insert(1, "NumPy")
print(a) # [10, 'RS', 'Python']
print(b) # [10, 'NumPy', 'RS']| Statement | Memory |
|---|---|
b = a |
Same object (alias) |
b = a.copy() |
Different object |
copy()performs a shallow copy — only the outer list is duplicated. See Aliasing and Copying for nested lists.
2. reverse() — flip the order
Syntax: listobj.reverse()
reverse() changes the same list in place.
lst = [10, 20, 30, 40]
lst.reverse()
print(lst) # [40, 30, 20, 10]x = lst.reverse()
print(x) # None
reverse()returns nothing. It only mutates the list.
3. sort() — arrange data
Syntax: listobj.sort() / listobj.sort(reverse=True)
Sorting works best on same-type homogeneous data.
Ascending sort
nums = [10, 2, 13, -12, 56, 3]
nums.sort()
print(nums) # [-12, 2, 3, 10, 13, 56]Descending order
nums.sort(reverse=True)
print(nums) # [56, 13, 10, 3, 2, -12]String sorting
names = ["Zoom", "Rossum", "Anil", "Rajesh", "Babu"]
names.sort()
print(names) # ['Anil', 'Babu', 'Rajesh', 'Rossum', 'Zoom']Mixed data?
data = [10, "Python", 20]
data.sort() # TypeError — cannot compare int with strsort() vs reverse()
Arranges values logically
Simply flips existing order
| Feature | sort() |
reverse() |
|---|---|---|
| Arranges values | Yes | No |
| Reverses order | No | Yes |
| Mutates list | Yes | Yes |
| Returns | None |
None |
4. del — delete elements
Unlike methods, del is a Python statement — not a list method.
Delete one element
lst = ["A", "B", "C", "D"]
del lst[2]
print(lst) # ['A', 'B', 'D']Delete a slice
lst = [10, "RS", 45.67, "Python", True, "HYD", 23.45]
del lst[3:6]
print(lst) # [10, 'RS', 45.67, 23.45]Delete the whole variable
lst = [1, 2, 3]
del lst
print(lst) # NameError: name 'lst' is not definedThe variable name disappears; the object becomes eligible for garbage collection.
| Use | Syntax |
|---|---|
| Delete one item | del lst[i] |
| Delete slice | del lst[a:b] |
| Delete name | del lst |
Reverse copy using slicing
Sometimes you want the original list untouched.
lst = [10, 20, 30]
rev = lst[::-1]
print(lst) # [10, 20, 30]
print(rev) # [30, 20, 10]
[::-1]creates a new list, whilereverse()modifies the original.
Mini project — leaderboard ranking
scores = [88, 95, 72, 95, 81]
backup = scores.copy() # Step 2: backup
scores.sort(reverse=True) # Step 3: sort descending
for rank, score in enumerate(scores, start=1):
print(rank, score)Visual leaderboard
This combines copy + sort + enumerate into one practical program. backup still holds the original unsorted scores.
One-page revision sheet
| Task | Best choice |
|---|---|
| Sort ascending | sort() |
| Sort descending | sort(reverse=True) |
| Reverse same list | reverse() |
| Reverse without changing original | [::-1] |
| Create independent list | copy() |
| Delete one item | del lst[i] |
| Delete range | del lst[a:b] |
| Delete variable | del lst |
Common interview mistakes
Mistake 1 — saving sort() result:
x = nums.sort() # x is None — wrong pattern
nums.sort()
print(nums) # correctMistake 2 — alias instead of copy:
b = a # same object — both change together
b = a.copy() # independent list — correctPractice questions
Test yourself before File 05 — Nested Lists:
- What is the difference between
reverse()and[::-1]? - Why does
sort()returnNone? - What happens after
b = aanda.append(5)? - Which statement creates an independent list?
- How do you delete indices
2through5?
Knowledge check
Q1: reverse() vs [::-1]?
Answer: reverse() mutates the same list; [::-1] returns a new reversed list.
Q2: Why does sort() return None?
Answer: It sorts in place — the list object is modified directly.
Q3: After b = a and a.append(5), what is b?
Answer: Same as a — alias shares one object.
Q4: Practice Q5 — delete indices 2 through 5?
Answer: del lst[2:6] (slice end is exclusive).
Loading playground…