1.2.22Introduction to Programming (Python)

List methods — append, insert, remove, pop, sort, reverse, count, index

1,649 words7 min readdifficulty · medium

WHY do these methods exist?

A list stores data in a sequence with positions called indices (starting at 0). Once you have data, you need to:

  1. Grow itappend, insert
  2. Shrink itremove, pop
  3. Reorder itsort, reverse
  4. Query itcount, index

Without these you'd have to rebuild the whole list by hand every time — slow and error-prone. These methods do the bookkeeping for you.


WHAT each method does (the contract)


HOW it works under the hood (derive the behaviour)

A list is laid out as positions 0,1,2,,n10, 1, 2, \dots, n-1 where n=n = len(lst).

append — derive its index. The end of a list of length nn is the next free slot: new index of appended item=n=len(lst)\text{new index of appended item} = n = \texttt{len(lst)} After appending, length becomes n+1n+1.

insert(i, x) — everything at position jij \ge i must move to j+1j+1 to make room: old item at j    new position j+1(ji)\text{old item at } j \;\longrightarrow\; \text{new position } j+1 \quad (j \ge i) This is why insert near the front is "slow" (many shifts) while append is "fast" (no shifts).

pop(i) — returns lst[i], then everything at j>ij > i moves to j1j-1 to close the gap. Length becomes n1n-1.

Figure — List methods — append, insert, remove, pop, sort, reverse, count, index

Worked examples


Recall Feynman: explain to a 12-year-old

Imagine a row of numbered lockers. append puts a bag in the next empty locker at the end. insert squeezes a bag into a chosen locker and pushes everyone after it down one. pop takes a bag out and hands it to you. remove finds a specific bag and throws it away (gives you nothing back). sort lines up all the bags from smallest to biggest, reverse flips the whole row. count tells you "how many red bags?" and index tells you "which locker is the first red bag in?" The hand-back-or-not difference is the whole trick: pop hands you something, the others don't.


Flashcards

What does list.append(x) return?
None — it mutates the list in place, adding x to the end.
Difference between remove and pop?
remove(value) deletes first matching value and returns None; pop(index) removes by index and returns the item.
What does pop() with no argument do?
Removes and returns the LAST element.
What does sort() return, and how to get a new sorted list instead?
Returns None (sorts in place); use sorted(lst) for a new list.
Does reverse() sort the list?
No — it only flips the current order; sort first if you want descending.
What happens with lst.remove(x) if x is not present?
Raises ValueError.
What does count(x) return?
The number of times x appears in the list.
What does index(x) return for a missing element?
Raises ValueError; otherwise the index of the first occurrence.
After [3,1,2].insert(1,9), what is the list?
[3, 9, 1, 2] — items at index ≥1 shift right.
How to sort descending in one call?
lst.sort(reverse=True).

Connections

  • Lists — creation and indexing
  • Mutability vs Immutability in Python
  • sorted() vs list.sort()
  • Tuples — why no append/sort
  • Time complexity — append O(1) vs insert O(n)
  • Strings — index and count methods
  • Exceptions — ValueError and IndexError

Concept Map

has

acted on by

add items

remove items

reorder

query

return

return

remove returns

pop returns

return

causes bug

fixed by

Mutable ordered list

Indices from 0

Eight list methods

append, insert

remove, pop

sort, reverse

count, index

Returns None mutates in place

Removed item value

Number or index

x = lst.sort gives None

Use sorted lst for new list

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek Python list ko ek numbered shelf samjho jisme har dabbe ka ek index hota hai (0 se start). Ye aath methods us shelf ke saath kaam karne ke verbs hain. append end me item daalta hai, insert(i, x) kisi position pe ghusata hai aur baaki sab ko ek step right shift kar deta hai. remove(value) ek specific value ko dhoondh ke hata deta hai par kuch wapas nahi karta, jabki pop(index) item ko nikaal ke tumhe wapas de deta hai — yahi sabse important difference hai: pop "pays you back", remove "robs silently".

Sabse common galti: x = lst.sort() likhna. sort(), append, insert, remove, reverse ye sab list ko jagah-pe (in place) badalte hain aur None return karte hain. Isliye agar tum return value assign karoge to None mil jaayega aur lagega data gaayab ho gaya. Naya sorted list chahiye to function sorted(lst) use karo, method nahi.

count(x) batata hai kitni baar value aayi, aur index(x) pehli occurrence ka position deta hai — agar value nahi mili to ValueError aata hai, dhyan rakhna. Aur reverse() sort nahi karta, sirf order ulta karta hai; descending chahiye to lst.sort(reverse=True) best hai.

Yeh chhota sa group of methods 80/20 wala hai — interviews, DSA, aur daily coding sabme bar-bar aate hain. Ek baar add/take/move/ask categories yaad ho gayi, to kabhi confuse nahi hoge.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)