Skip to content
PythonWorking with Collectionsbeginner18 min read

List Methods and Mutation

Learn how to add, insert, remove, and retrieve list elements using Python's built-in list methods.

In this lesson, you will learn

  • ✓Explain the behavior of append(), extend(), insert(), remove(), and pop().
  • ✓Distinguish between adding one element and adding multiple elements to a list.
  • ✓Explain list mutation and modify elements using indexes.
  • ✓Predict the state of a list after a sequence of operations.
  • ✓Handle common list-operation errors, including ValueError and IndexError.

1. Understanding list mutation

Mutation means changing an existing object rather than creating a separate object for every change. Python lists are mutable, so you can add or remove elements and replace existing elements after creating a list.

For example, values[0] = 10 replaces the element at index 0 with 10. The list keeps its identity, but its contents change.

List methods also mutate lists when they add or remove elements. The important difference is that each method performs a specific operation. Understanding that operation helps you choose the correct method for a task.

Changing an existing element

Changing an existing element
python
numbers = [2, 4, 6]
numbers[1] = 10
print(numbers)
Output
[2, 10, 6]

The original list contains 2, 4, and 6. The index 1 refers to the second element, which is 4. The assignment replaces 4 with 10 without adding or removing an element. The resulting list is [2, 10, 6].

2. Adding elements: append(), extend(), and insert()

Use append(x) to add one element to the end of a list. The argument becomes a single new element, even when the argument is itself a list.

Use extend(iterable) to add the elements from an iterable, such as another list, to the end of the existing list. Unlike append(), extend() adds the iterable's elements individually.

Use insert(index, x) to add one element at a specified position. Elements at that position and later positions shift to the right. If the index is greater than or equal to the list length, the element is inserted at the end; a negative index is interpreted relative to the list, with insertion occurring no earlier than the beginning.

append() adds one element

append() adds one element
python
values = [2, 4]
values.append(6)
values.append([8, 10])
print(values)
Output
[2, 4, 6, [8, 10]]

The first append() adds the integer 6 as one element. The second append() adds the entire list [8, 10] as one nested element. The original list now contains four elements, and its last element is another list.

Note

append(), extend(), and insert() modify the existing list and return None. To keep the modified list, call the method on the list itself instead of assigning the method's return value back to the list.

3. Removing elements: remove() and pop()

Use remove(x) to remove the first element equal to x. The argument is a value, not an index. If the value occurs more than once, only its first occurrence is removed.

If the specified value does not occur in the list, remove() raises ValueError. If you need to check whether a value exists before removing it, use an if condition.

Use pop() to remove and return an element. Without an argument, pop() removes and returns the last element. With an index, it removes and returns the element at that index.

Calling pop() on an empty list, or using an invalid index, raises IndexError. When you need both the removed element and the updated list, pop() is often the appropriate choice.

remove() removes the first matching value

remove() removes the first matching value
python
numbers = [3, 5, 3, 7]
numbers.remove(3)
print(numbers)
Output
[5, 3, 7]

remove(3) searches for the first element equal to 3. The first 3 is removed; the second 3 remains. The resulting list is [5, 3, 7].

4. Step-by-step dry run

A dry run means tracing a program statement by statement without skipping intermediate changes. After each operation, write down the current list and any returned value.

The following example combines append(), insert(), remove(), and pop(). Track the list after each statement to understand how the operations interact.

Combining list methods

Combining list methods
python
values = [2, 4]
values.append(6)
values.insert(1, 3)
values.remove(4)
removed = values.pop()
print(values)
print(removed)
Output
[2, 3, 6] 6

Start with values = [2, 4]. After append(6), values is [2, 4, 6]. After insert(1, 3), values is [2, 3, 4, 6]. After remove(4), values is [2, 3, 6]. pop() removes and returns 6. The list becomes [2, 3], and removed stores 6. The first print() therefore displays [2, 3], and the second displays 6.

Note

A list method's effect on the list and its return value are separate things. For example, pop() both changes the list and returns the removed element, while append() changes the list but returns None.

5. Practical use and edge cases

List methods are useful when maintaining a collection of tasks, scores, names, or other values. Choose a method according to whether you need to add an element, add several elements, insert at a position, remove by value, or remove and retrieve by position.

Check boundary cases before removing elements. remove() raises ValueError when the value is absent, while pop() raises IndexError when the list is empty or the specified index is invalid.

An empty-list check can prevent pop() from being called when there is nothing to remove. A membership check can prevent remove() from being called for a value that is absent.

Avoid assigning the result of an in-place list method such as append() or extend() back to the list. These methods return None, so such an assignment would replace your list variable with None.

Check before removing a value

Check before removing a value
python
names = ['Asha', 'Ravi', 'Mina']
name = 'Ravi'
 
if name in names:
names.remove(name)
 
print(names)
Output
['Asha', 'Mina']

The membership expression checks whether 'Ravi' occurs in names. Because the value exists, remove() removes its first occurrence. The updated list is ['Asha', 'Mina'].

Caution

Do not confuse a value with an index. remove(2) removes the first element equal to 2, while pop(2) removes the element at index 2. These operations can produce different results.

Common pitfalls to avoid

Real debugging lessons and exact syntax contrasts to avoid runtime bugs

#1 Confusing append() with extend().

Why it happens: append(item) adds its argument as one element, while extend(iterable) adds each item from the iterable separately.

Incorrect Approach
script.py
python
Confusing append() with extend().
Correct Solution
script.py
python
append([3, 4]) adds one nested list element, while extend([3, 4]) adds 3 and 4 as separate elements.

Key takeaway: append([3, 4]) adds one nested list element, while extend([3, 4]) adds 3 and 4 as separate elements.

#2 Using remove(index) when intending to remove an element by position.

Why it happens: remove(value) removes by matching value; pop(index) removes by position, so use pop(index) when the index is known.

Incorrect Approach
script.py
python
Using remove(index) when intending to remove an element by position.
Correct Solution
script.py
python
remove(x) searches for a value. Use pop(index) to remove an element by index.

Key takeaway: remove(x) searches for a value. Use pop(index) to remove an element by index.

#3 Assuming pop() only removes an element.

Why it happens: pop() both removes and returns an element, allowing the removed value to be stored or used.

Incorrect Approach
script.py
python
Assuming pop() only removes an element.
Correct Solution
script.py
python
pop() also returns the removed element. Store that return value if you need to use it later.

Key takeaway: pop() also returns the removed element. Store that return value if you need to use it later.

#4 Assuming remove() removes every matching value.

Why it happens: remove(value) removes only the first matching occurrence; filter or loop when every occurrence should be removed.

Incorrect Approach
script.py
python
Assuming remove() removes every matching value.
Correct Solution
script.py
python
remove(x) removes only the first matching occurrence. Repeat the operation or use another appropriate approach if all occurrences must be removed.

Key takeaway: remove(x) removes only the first matching occurrence. Repeat the operation or use another appropriate approach if all occurrences must be removed.

#5 Assigning the result of append() to the same list variable.

Why it happens: append() mutates the list in place and returns None, so assigning its result back to the list replaces the variable with None.

Incorrect Approach
script.py
python
Assigning the result of append() to the same list variable.
Correct Solution
script.py
python
Write values.append(5) on its own line. Since append() returns None, values = values.append(5) would replace values with None.

Key takeaway: Write values.append(5) on its own line. Since append() returns None, values = values.append(5) would replace values with None.

#6 Calling remove() or pop() without checking a relevant boundary case.

Why it happens: remove() raises ValueError when the value is absent, and pop() raises IndexError when the list or requested index is invalid.

Incorrect Approach
script.py
python
Calling remove() or pop() without checking a relevant boundary case.
Correct Solution
script.py
python
Check membership before remove() when a value might be absent, and check whether a list is non-empty before pop() when it might be empty.

Key takeaway: Check membership before remove() when a value might be absent, and check whether a list is non-empty before pop() when it might be empty.

Ready for hands-on challenges?

Test your skills with shuffled coding challenges in the Practice Arena.

Practice Arena →

Technical and placement interview questions

Frequently tested interview questions with detailed, full answers (click to expand)

Q1:What is the difference between append() and extend()?
Answer:
append(x) adds x as one element at the end of a list. extend(iterable) adds the elements from an iterable individually. For example, appending [3, 4] to [1, 2] produces [1, 2, [3, 4]], while extending it produces [1, 2, 3, 4].
Q2:What is the difference between remove() and pop()?
Answer:
remove(x) searches for and removes the first element equal to x. pop(index) removes and returns the element at the specified index; pop() without an index removes and returns the last element. remove() raises ValueError when the value is absent, while pop() raises IndexError for an empty list or an invalid index.
Q3:What does it mean that Python lists are mutable?
Answer:
A list is mutable because its contents can be changed after the list is created. You can replace an element using index assignment, or add and remove elements using list methods, without needing to create a new list for each change.
Q4:What do append(), extend(), and insert() return?
Answer:
These methods modify the list in place and return None. Their purpose is to change the existing list, not to produce a new list as their return value.
Q5:How can you safely remove an element when the value might not exist?
Answer:
Check membership first, such as if value in values:, and call values.remove(value) only when the condition is true. Alternatively, handle ValueError when that better fits the program's requirements.

Key takeaways

  • Lists are mutable, so you can replace elements and change the number of elements after creation.
  • append() adds one element, extend() adds elements from an iterable, and insert() adds one element at a specified position.
  • remove() deletes the first matching value, while pop() removes and returns an element by index or removes the last element when no index is supplied.
  • List methods that mutate the list, such as append(), extend(), insert(), and remove(), return None.
  • remove() can raise ValueError, and pop() can raise IndexError. Check relevant edge cases when necessary.
  • Trace each operation in order to predict the updated list and any returned value.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: