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

Lesson 02

List Methods — append(), insert(), remove(), pop(), clear()

Master Python list mutation methods: append and insert at any index, remove by value, pop by index, and clear all elements in place.

Intermediate28 min

What you will learn

Master the 5 most important list mutation methods: append(), insert(), remove(), pop(), and clear(). These methods modify the same list object in memory — the id() stays the same.

Method Action
append() Add one item at the end
insert() Add at any index (+ve or −ve)
remove() Delete first match by value
pop() Delete by index and return the item
clear() Remove all elements

Part of Collections lesson 2 · Review list basics: list data type.

Learning roadmap

append insert remove pop clear — list mutation flow

Memory trick: Add → Insert → Remove → Pop → Clear

1. append() — add at the end

Syntax: listobj.append(value)

Before and after append(30)
python
numbers = [10, 20]
numbers.append(30)
print(numbers)  # [10, 20, 30]

Remember:

  • Always adds one item
  • Adds to the last position
  • Original list changes — same id()

Build from empty:

python
lst = []
lst.append(10)
lst.append(20)
lst.append(10)  # duplicates allowed
print(lst)  # [10, 20, 10]

2. insert() — add anywhere

Syntax: listobj.insert(index, value)

Positive index

insert(2, RS) — slot opens at index 2

Result

List after insert at index 2
python
lst = [10, 20, 30, 40]
lst.insert(2, "RS")
print(lst)  # [10, 20, 'RS', 30, 40]

Negative index and edge cases

Code Meaning
insert(-1, x) Before last element
insert(0, x) Beginning
insert(999, x) Past end → same as append
insert(-400, x) Before start → prepend
python
lst = [10, 20, 30, 40, 50]
lst.insert(-1, 155)   # before last
lst.insert(300, "HYD")    # invalid +ve → end
lst.insert(-400, "BANG")  # invalid −ve → start

3. remove() — delete by value

Syntax: listobj.remove(value)

remove(10) — first matching 10 only
python
lst = [10, 20, 10, 30]
lst.remove(10)
print(lst)  # [20, 10, 30]

Only the first 10 disappears. Raises ValueError if the value is missing.

Interview question: remove() works with values, not indexes.

4. pop() — delete by index

Two ways to use it

pop() removes last item
pop(1) removes item at index 1
python
fruits = ["Apple", "Banana", "Orange"]
item = fruits.pop(1)
print(item)    # Banana
print(fruits)  # ['Apple', 'Orange']

last = fruits.pop()  # no arg → last item

Errors: IndexError on empty list or bad index.

Important difference

Method Removes by Returns
remove() Value None
pop() Index Removed item

5. clear() — empty the list

Syntax: listobj.clear()

Before clear() vs empty list after
python
lst = [10, 20, 30]
lst.clear()
print(lst)  # []

The list becomes empty, but it is still the same object in memory (id() unchanged). Returns None.

One-page comparison sheet

append insert remove pop clear quick reference
Method Removes? Returns On missing / empty
append(v) No None —
insert(i, v) No None —
remove(v) Yes (first) None ValueError
pop() Yes (last) value IndexError
pop(i) Yes (at i) value IndexError
clear() All None safe on []

Mini project — student attendance system

python
attendance = ["Asha", "Ravi", "Meera"]

attendance.append("Kiran")      # late arrival
attendance.remove("Ravi")       # left early
attendance.insert(1, "Priya")   # new student

last = attendance.pop()         # undo last entry

print(attendance)
print(last)

Execution flow

Attendance list: append remove insert pop flow

This exercise combines append → remove → insert → pop, exactly like a real application.

Interview quick revision

Method Use this when… Example
append() Add one item to end Notifications queue
insert() Place item at position Priority queue
remove() Delete known value Remove username
pop() Delete by index and get value Undo feature
clear() Reset everything Logout / reset form

Practice questions

Test yourself before File 03 — Search and Extend:

  1. What is the difference between append() and insert()?
  2. Why does remove(10) delete only one value?
  3. Which method returns the deleted element?
  4. What happens if pop() is called on an empty list?
  5. Does clear() create a new list object?

Knowledge check

Q1: lst = [1, 2, 3]; lst.append([4, 5]) — what is lst?
Answer: [1, 2, 3, [4, 5]] — one nested list appended as a single element.

Q2: Difference between remove(10) and pop(0)?
Answer: remove finds value 10; pop(0) removes whatever is at index 0.

Q3: Practice Q4 — pop() on []?
Answer: IndexError: pop from empty list.

Q4: Practice Q5 — does clear() create a new list?
Answer: No — same object, zero elements. id(lst) unchanged.

Loading playground…