Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate28 min

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

copy sort reverse del — organize and delete flow

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)

Alias shares one object vs copy creates two objects
python
a = [10, "RS"]
b = a.copy()
print(a is b)  # False

After modifying one list:

python
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.

Before and after reverse()
python
lst = [10, 20, 30, 40]
lst.reverse()
print(lst)  # [40, 30, 20, 10]
python
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

sort() arranges values ascending
python
nums = [10, 2, 13, -12, 56, 3]
nums.sort()
print(nums)  # [-12, 2, 3, 10, 13, 56]

Descending order

python
nums.sort(reverse=True)
print(nums)  # [56, 13, 10, 3, 2, -12]

String sorting

python
names = ["Zoom", "Rossum", "Anil", "Rajesh", "Babu"]
names.sort()
print(names)  # ['Anil', 'Babu', 'Rajesh', 'Rossum', 'Zoom']

Mixed data?

python
data = [10, "Python", 20]
data.sort()  # TypeError — cannot compare int with str

sort() vs reverse()

sort() arranges values logically

Arranges values logically

reverse() flips existing order

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

del lst[2] removes index 2
python
lst = ["A", "B", "C", "D"]
del lst[2]
print(lst)  # ['A', 'B', 'D']

Delete a slice

python
lst = [10, "RS", 45.67, "Python", True, "HYD", 23.45]
del lst[3:6]
print(lst)  # [10, 'RS', 45.67, 23.45]
Delete indices 3 through 5

Delete the whole variable

python
lst = [1, 2, 3]
del lst
print(lst)  # NameError: name 'lst' is not defined

The 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.

python
lst = [10, 20, 30]
rev = lst[::-1]
print(lst)  # [10, 20, 30]
print(rev)  # [30, 20, 10]
Original list vs new reversed slice copy

[::-1] creates a new list, while reverse() modifies the original.

Mini project — leaderboard ranking

python
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

Sorted leaderboard ranks 1 through 5

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:

python
x = nums.sort()  # x is None — wrong pattern
nums.sort()
print(nums)      # correct

Mistake 2 — alias instead of copy:

python
b = a            # same object — both change together
b = a.copy()     # independent list — correct

Practice questions

Test yourself before File 05 — Nested Lists:

  1. What is the difference between reverse() and [::-1]?
  2. Why does sort() return None?
  3. What happens after b = a and a.append(5)?
  4. Which statement creates an independent list?
  5. How do you delete indices 2 through 5?

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…