01 / 23

Python
List Methods

Master the essential methods to add, remove, and slice elements in Python lists: the building blocks of dynamic data.

Python

What Is a List?

A list is Python's most versatile data structure: an ordered, mutable collection of items. Lists can hold any data type and can grow or shrink as your program runs.

Python
fruits = ["apple", "banana", "cherry"]
nums   = [10, 20, 30, 40]

Index Positions

Every element in a list has a position called an index. Python uses zero-based indexing: the first element is at index 0.

"apple" "banana" "cherry" "date" ... 0 1 2 3 -4 -3 -2 -1

Why List Methods Matter

Raw assignment can only replace elements. List methods let you dynamically grow, shrink, reorder, and extract data: the operations behind every real-world program.

Adding
append, extend, insert
Removing
pop, remove
Extracting
Slicing with [start:stop:step]

.append(): Add One Item

append(item) adds a single element to the end of the list. The list grows by exactly one.

Python
nums = [1, 2, 3]
nums.append(4)
print(nums)  # [1, 2, 3, 4]
BEFORE 1 2 3 → AFTER 1 2 3 4 0 1 2 3

.extend(): Add Multiple Items

extend(iterable) adds each element of an iterable to the end of the list. The list grows by the length of that iterable.

Python
nums = [1, 2]
nums.extend([3, 4, 5])
print(nums)  # [1, 2, 3, 4, 5]
BEFORE 1 2
+
3 4 5 → RESULT: 5 items [1, 2, 3, 4, 5]

.insert(): Add at a Position

insert(index, item) places an element at the given index, shifting all later elements one position to the right.

Python
colors = ["red", "blue", "green"]
colors.insert(1, "yellow")
print(colors)
# ["red", "yellow", "blue", "green"]
BEFORE "red" "blue" "green" 0 1 2 ↓ AFTER "red" "yellow" "blue" "green"

append vs extend

The most common confusion: append adds the entire object as one element, while extend unpacks it element by element.

Python
a = [1, 2]
a.append([3, 4])
print(a)   # [1, 2, [3, 4]]  ← nested list!

b = [1, 2]
b.extend([3, 4])
print(b)   # [1, 2, 3, 4]    ← flat list!
append(x) extend(x)
Adds x as one element Each item in x
Length change +1 +len(x)
Accepts Any object Iterables only

.pop(): Remove and Return

pop(index) removes the element at the given index and returns it. Without an argument, it removes the last element.

Python
stack = ["a", "b", "c", "d"]

last = stack.pop()       # "d"
print(stack)              # ["a", "b", "c"]

second = stack.pop(1)   # "b"
print(stack)              # ["a", "c"]

.remove(): Delete by Value

remove(value) finds the first occurrence of a value and deletes it. It raises a ValueError if the value is not found.

Python
letters = ["x", "y", "z", "y"]
letters.remove("y")
print(letters)  # ["x", "z", "y"]
# Only the FIRST "y" was removed
pop(i)
Remove by index. Returns the removed item.
remove(v)
Remove by value. Returns None.

List Slicing

Slicing extracts a sub-list using the syntax list[start:stop:step]. It returns a new list without modifying the original.

Python
nums = [10, 20, 30, 40, 50, 60]

nums[1:4]     # [20, 30, 40]
nums[:3]      # [10, 20, 30]
nums[3:]      # [40, 50, 60]
nums[::2]     # [10, 30, 50]
nums[::-1]    # [60, 50, 40, 30, 20, 10]

Slicing Visualized

10 20 30 40 50 60 0 1 2 3 4 5 [1:4] → [20, 30, 40] [:3] → [10, 20, 30] 10 20 30 10 30 50 [::2] → [10, 30, 50] [::-1] → [60, 50, 40, 30, 20, 10] (reversed)

How to Choose the Right Method

1
Need to add one item to the end? Use append(item).
2
Need to merge two lists? Use extend(iterable) or the + operator.
3
Need to add at a specific position? Use insert(i, item).
4
Need to remove by position and get the value? Use pop(i).
5
Need to remove a specific value? Use remove(value).
6
Need a sub-list or a reversed copy? Use list[start:stop:step].

Example: Building a Playlist

Python
playlist = ["Song A", "Song B"]

# Add a song at the end
playlist.append("Song C")
# ["Song A", "Song B", "Song C"]

# Insert a favorite at position 1
playlist.insert(1, "Song D")
# ["Song A", "Song D", "Song B", "Song C"]

# Remove a song you don't like
playlist.remove("Song B")
# ["Song A", "Song D", "Song C"]

# Pop the last song to play it
now_playing = playlist.pop()
print(now_playing)  # "Song C"
print(playlist)     # ["Song A", "Song D"]

Example: Slice Operations

Python
data = [5, 10, 15, 20, 25, 30, 35, 40]

# First three elements
first_three = data[:3]
# [5, 10, 15]

# Last three elements
last_three = data[-3:]
# [30, 35, 40]

# Every other element starting from index 1
odds = data[1::2]
# [10, 20, 30, 40]

# Reverse the list
rev = data[::-1]
# [40, 35, 30, 25, 20, 15, 10, 5]

Example: Slice Assignment

You can also assign to a slice, replacing a range of elements with new values. The replacement does not need to be the same length.

Python
nums = [0, 1, 2, 3, 4, 5]

# Replace indices 2-4 with new values
nums[2:5] = [20, 30]
print(nums)  # [0, 1, 20, 30, 5]

# Delete a range (assign empty list)
nums[1:3] = []
print(nums)  # [0, 30, 5]

Common Mistakes

✕ Assigning the return of append
x = lst.append(5) sets x to None, not the updated list. append modifies in place.
✕ Using append instead of extend
lst.append([3,4]) nests a list inside the list. Use extend to add each item.
✕ Removing while iterating
Calling remove() inside a for loop on the same list skips elements. Iterate over a copy instead.
✕ Off-by-one in slicing
lst[1:3] gives indices 1 and 2, not 1, 2, and 3. The stop index is always excluded.

Method Summary

Method What It Does Returns
append(x) Add x to end None
extend(iter) Add all items of iter to end None
insert(i, x) Insert x before index i None
pop(i) Remove and return item at i The item
remove(x) Remove first occurrence of x None
[s:e:st] Extract sub-list (copy) New list

Question 1 of 5

What is the output of the following code?
x = [1, 2, 3]; x.append([4, 5]); print(len(x))

Question 2 of 5

Given m = [10, 20, 30, 40, 50], what does m[1:4] return?

Question 3 of 5

Which method should you use to remove an item by its value without needing the return value?

Question 4 of 5

What does [1, 2, 3, 4, 5][::-1] produce?

Question 5 of 5

What is the value of result after this code runs?
lst = ["a", "b", "c"]; result = lst.pop(0)