In Post #49, we established that lists are mutable, meaning we can change them after they are created. This ability to grow and shrink is what makes them so useful for managing dynamic collections of data. But how exactly do we add new items to a list?
In this post, we’ll explore the three primary built-in methods for adding elements to a list: .append()
, .insert()
, and .extend()
. We’ll learn what each one does and, more importantly, when to use each one.
A method is like a function that belongs to an object. We call it using dot notation, like my_list.append(new_item)
, to perform an action on that specific object.
.append()
: Adding an Item to the End
The .append()
method is the most common and straightforward way to add to a list. It adds a single item to the very end of the list.
The syntax is: my_list.append(item_to_add)
This is perfect for when you’re collecting items and want to add them in the order you receive them. Let’s build a shopping list.
shopping_list = ["milk", "eggs"]
print(f"Initial list: {shopping_list}")
shopping_list.append("bread")
print(f"After appending 'bread': {shopping_list}")
shopping_list.append("apples")
print(f"After appending 'apples': {shopping_list}")
The output shows the list growing with each append:
Initial list: ['milk', 'eggs']
After appending 'bread': ['milk', 'eggs', 'bread']
After appending 'apples': ['milk', 'eggs', 'bread', 'apples']
.insert()
: Adding an Item at a Specific Position
What if you need more control over where the new item goes? The .insert()
method allows you to add an item at any specific index in the list. All the existing items from that index onward are shifted one position to the right to make room.
The syntax is: my_list.insert(index, item_to_add)
Let’s say we have a list of tasks and need to add a high-priority item to the beginning.
tasks = ["Call mom", "Do laundry"]
print(f"Initial tasks: {tasks}")
# Insert a high-priority task at the beginning (index 0)
tasks.insert(0, "Pay bills")
print(f"After inserting at index 0: {tasks}")
# Insert another task in the middle (at index 2)
tasks.insert(2, "Go to the bank")
print(f"After inserting at index 2: {tasks}")
The output shows how the list expands and shifts:
Initial tasks: ['Call mom', 'Do laundry']
After inserting at index 0: ['Pay bills', 'Call mom', 'Do laundry']
After inserting at index 2: ['Pay bills', 'Call mom', 'Go to the bank', 'Do laundry']
.extend()
: Merging a List with Another
The .extend()
method is used when you want to add all the items from another list (or any other iterable) to the end of your current list. This is also known as “merging” or “concatenating” lists.
The syntax is: my_list.extend(another_list)
first_half = [1, 2, 3]
second_half = [4, 5, 6]
print(f"First half before extend: {first_half}")
# Extend the first list with all items from the second list
first_half.extend(second_half)
print(f"First half after extend: {first_half}")
The result is one single, merged list:
First half before extend: [1, 2, 3]
First half after extend: [1, 2, 3, 4, 5, 6]
A Critical Distinction: .extend()
vs. .append()
This is a very common point of confusion for beginners. What happens if you try to use .append()
to add a list to another list?
list_a = [1, 2, 3]
list_b = [4, 5]
list_a.append(list_b)
print(f"Using .append() with a list: {list_a}")
The output is: Using .append() with a list: [1, 2, 3, [4, 5]]
.
Notice that .append()
added the entire list_b
as a single item at the end of list_a
. This creates a nested list.
- Use
.extend()
when you want to add the individual elements of a list. - Use
.append()
when you want to add the entire list as one element.
What’s Next?
You now have three ways to grow a list: use .append()
to add one item to the end, .insert()
to add one item at a specific position, and .extend()
to add all items from another list to the end. Choosing the right method makes your intent clear and your code effective.
Growing lists is only half the story. Just as often, we need to remove items. In Post #51, we will look at the other side of mutability and learn the different ways to shrink a list.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com