"Every Python list method (append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy) with runnable examples, their output, and the bugs they cause."
tags: python, beginners, tutorial, programming

Python lists have 11 methods. Most tutorials list them and move on. This one shows what each method prints, what it returns, and the mistake people usually make with it, because the mistakes are what you end up searching for at 11 pm.

Every example runs as-is in Python 3. Paste it into a file or the REPL and check the output yourself.

Quick reference

Method What it does Returns Changes the list?
append(x) Adds x to the end None Yes
extend(iterable) Adds each item of iterable to the end None Yes
insert(i, x) Puts x before position i None Yes
remove(x) Deletes the first item equal to x None Yes
pop(i=-1) Deletes and returns the item at i The item Yes
clear() Deletes every item None Yes
index(x) Finds the position of the first x int No
count(x) Counts how many items equal x int No
sort() Sorts the list in place None Yes
reverse() Reverses the list in place None Yes
copy() Makes a shallow copy A new list No

Notice how many return None. That one fact explains half the bugs below.

How do you add items to a list in Python?

Use append() to add one item to the end, extend() to add several, and insert() to add one item at a specific position.

append()

fruits = ["apple", "banana"] fruits.append("cherry") print(fruits) 

Output:

['apple', 'banana', 'cherry'] 

What is the difference between append and extend?

append() adds its argument as one item, even if that argument is a list. extend() loops over its argument and adds each item.

a = [1, 2] a.append([3, 4]) print(a) b = [1, 2] b.extend([3, 4]) print(b) 

Output:

[1, 2, [3, 4]] [1, 2, 3, 4] 

Common mistake: a string is iterable, so extend() splits it into characters.

tags = ["python"] tags.extend("ai") print(tags) 

Output:

['python', 'a', 'i'] 

Use tags.append("ai") to add the whole word.

insert()

insert(i, x) puts x before index i. An index past the end doesn't raise an error. Python adds the item at the end instead.

nums = [10, 30] nums.insert(1, 20) print(nums) nums.insert(100, 40) print(nums) 

Output:

[10, 20, 30] [10, 20, 30, 40] 

How do you remove items from a list in Python?

Use remove() to delete by value, pop() to delete by position and get the item back, and clear() to empty the list.

remove()

remove() deletes only the first match.

colors = ["red", "blue", "red"] colors.remove("red") print(colors) 

Output:

['blue', 'red'] 

If the value isn't in the list, you get an error:

colors.remove("green") 
ValueError: list.remove(x): x not in list 

Check first with if "green" in colors: when you aren't sure.

pop()

pop() removes the last item and returns it. Pass an index to remove a different item.

stack = [1, 2, 3] last = stack.pop() print(last, stack) first = stack.pop(0) print(first, stack) 

Output:

3 [1, 2] 1 [2] 

Calling pop() on an empty list raises IndexError: pop from empty list.

clear()

items = [1, 2, 3] items.clear() print(items) 

Output:

[] 

How do you find an item in a list?

index()

letters = ["a", "b", "c", "b"] print(letters.index("b")) 

Output:

1 

It returns the first position only. A missing value raises ValueError: 'z' is not in list. To just check whether something exists, "z" in letters is clearer and never raises.

count()

votes = ["yes", "no", "yes", "yes"] print(votes.count("yes")) 

Output:

3 

How do you sort a list in Python?

sort() sorts the list in place. Use reverse=True for descending order and key= to sort by something other than the raw value.

scores = [42, 7, 19] scores.sort() print(scores) scores.sort(reverse=True) print(scores) 

Output:

[7, 19, 42] [42, 19, 7] 

Strings sort by character code, so capital letters come before lowercase ones. Use key=str.lower to sort the way a person would:

words = ["banana", "apple", "Cherry"] words.sort() print(words) words.sort(key=str.lower) print(words) 

Output:

['Cherry', 'apple', 'banana'] ['apple', 'banana', 'Cherry'] 

sort() vs sorted()

list.sort() sorted(iterable)
Changes the original Yes No
Returns None A new list
Works on Lists only Any iterable (tuples, sets, dict keys, strings)

Common mistake: assigning the result of sort().

nums = [3, 1, 2] nums = nums.sort() print(nums) 

Output:

None 

You just replaced your list with None. Write nums.sort() on its own line, or nums = sorted(nums).

reverse()

steps = [1, 2, 3] steps.reverse() print(steps) 

Output:

[3, 2, 1] 

reversed(steps) and steps[::-1] give you a reversed version without changing the original.

How do you copy a list in Python?

This is the bug that confuses almost everyone once. b = a does not copy a list. It gives the same list a second name.

a = [1, 2, 3] b = a b.append(4) print(a) 

Output:

[1, 2, 3, 4] 

copy() makes a new list:

a = [1, 2, 3] c = a.copy() c.append(4) print(a, c) 

Output:

[1, 2, 3] [1, 2, 3, 4] 

Shallow copy vs deep copy

copy() is shallow. It copies the outer list, but lists inside it are still shared:

grid = [[1, 2], [3, 4]] g2 = grid.copy() g2[0].append(99) print(grid) 

Output:

[[1, 2, 99], [3, 4]] 

For nested lists, use copy.deepcopy():

import copy grid = [[1, 2], [3, 4]] g3 = copy.deepcopy(grid) g3[0].append(99) print(grid) 

Output:

[[1, 2], [3, 4]] 

The loop bug: removing items while iterating

nums = [1, 2, 2, 3] for n in nums: if n == 2: nums.remove(n) print(nums) 

Output:

[1, 2, 3] 

One 2 survived. Removing an item shifts everything after it one place left, and the loop's position moves forward anyway, so it skips the item that slid into the gap. Build a new list instead:

nums = [1, 2, 2, 3] nums = [n for n in nums if n != 2] print(nums) 

Output:

[1, 3] 

Which list methods are slow?

Lists are arrays underneath, so the end of the list is cheap and the front is expensive.

Operation Speed
append(), pop() Fast, constant time
insert(0, x), pop(0) Slow on big lists: every item shifts
remove(x), index(x), count(x), x in list Slow on big lists: scans every item

If you add and remove at the front a lot, use collections.deque, which has fast appendleft() and popleft(). If you check x in items a lot, use a set.

Quick answers

Does append return the new list?
No. append() changes the list and returns None.

How do I add two lists together?
a + b makes a new list. a.extend(b) adds b to a in place.

How do I remove all copies of a value?
Use a list comprehension: [x for x in items if x != value].

How do I get the last item without removing it?
items[-1]. Use pop() only when you also want it gone.

If this helped, the full beginner Python course on TutorialsHub covers lists, dictionaries, loops and functions the same way, chapter by chapter, at the level and length you choose. It's free.

What list bug cost you the most time? Tell me in the comments.