Python
List Methods
Master the essential methods to add, remove, and slice elements in Python lists: the building blocks of dynamic data.
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.
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.
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.
.append(): Add One Item
append(item) adds a single element to the end of the list. The list grows by exactly one.
nums = [1, 2, 3] nums.append(4) print(nums) # [1, 2, 3, 4]
.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.
nums = [1, 2] nums.extend([3, 4, 5]) print(nums) # [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.
colors = ["red", "blue", "green"] colors.insert(1, "yellow") print(colors) # ["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.
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.
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.
letters = ["x", "y", "z", "y"] letters.remove("y") print(letters) # ["x", "z", "y"] # Only the FIRST "y" was removed
List Slicing
Slicing extracts a sub-list using the syntax list[start:stop:step]. It returns a new list without modifying the original.
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
How to Choose the Right Method
Example: Building a Playlist
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
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.
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
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)