Advertisement
You’re working with lists in Python and need to add more items to an existing one. You know about append(), but you want to add several elements at once, not as a single item, but each one individually. That's where extend() comes in. It takes another list—or any iterable—and adds its contents to your current list. Unlike append(), it doesn’t add the entire object as one element. Instead, it stretches your list out, one item at a time. If you’ve ever felt unsure about how to combine lists the right way, this method is one of the cleanest solutions.
The extend() method is built into Python’s list object. It allows you to expand a list by adding the contents of another iterable. An iterable could be a list, a tuple, a set, or even a string. What makes extend() useful is that it takes each item from the iterable and adds it individually to the list—not as a group.
Here’s how it works in a basic case:
python
CopyEdit
colors = ['red', 'blue']
more_colors = ['green', 'yellow']
colors.extend(more_colors)
print(colors) # ['red', 'blue', 'green', 'yellow']
Rather than adding more_colors as one element, Python unpacks it and adds each color to the original list.
The general syntax is:
python
CopyEdit
your_list.extend(iterable)
This modifies the list in place and doesn’t return anything. That last part is important—extend() always returns None.
You can use any iterable. Here's a quick run with a tuple:
python
CopyEdit
nums = [1, 2]
nums.extend((3, 4))
print(nums) # [1, 2, 3, 4]
And even a string:
python
CopyEdit
letters = ['a']
letters.extend('bc')
print(letters) # ['a', 'b', 'c']
In all these cases, the method breaks the iterable into its items and adds them to the end of the list.
This method is designed for simplicity. You call it on a list, pass an iterable, and your list grows.
python
CopyEdit
a = [10, 20]
b = [30, 40]
a.extend(b)
print(a) # [10, 20, 30, 40]
python
CopyEdit
letters = ['x']
letters.extend('yz')
print(letters) # ['x', 'y', 'z']
python
CopyEdit
items = [1]
items.extend({2, 3})
print(items) # [1, 2, 3] (order may vary)
python
CopyEdit
info = ['name']
info.extend({'age': 25, 'gender': 'M'})
print(info) # ['name', 'age', 'gender']
In this case, only the keys from the dictionary are used, since iterating over a dictionary returns its keys by default.
Let’s say you have a list of pairs, and you want a flat list.
python
CopyEdit
pairs = [[1, 2], [3, 4]]
flat = []
for pair in pairs:
flat.extend(pair)
print(flat) # [1, 2, 3, 4]
This is a practical way to flatten shallow nested structures.
python
CopyEdit
start = []
start.extend([5, 6])
print(start) # [5, 6]
The method doesn’t require a pre-filled list. You can build one up from scratch.
python
CopyEdit
result = []
for i in range(3):
result.extend([i])
print(result) # [0, 1, 2]
Each value from the loop adds an item to the list.
Each of these examples shows a clear case of how extend() adds values in place and never returns a new list.
It’s easy to mix up extend() with append()—they sound similar but behave differently.
Let’s look at them side by side:
python
CopyEdit
a = [1, 2]
a.append([3, 4])
print(a) # [1, 2, [3, 4]]
Here, append() treats the second list as a single element.
Now compare:
python
CopyEdit
b = [1, 2]
b.extend([3, 4])
print(b) # [1, 2, 3, 4]
With extend(), the values go in one by one. This makes it a better option when you're looking to merge content rather than store grouped items.
If you're adding one item, use append():
python
CopyEdit
numbers = [5]
numbers.append(6)
But if you’re adding multiple items:
python
CopyEdit
numbers.extend([7, 8])
Understanding this difference will help you avoid accidentally creating nested lists when you just want flat ones.
The extend() method doesn’t create a new list—it changes the original one. That matters when multiple variables refer to the same list.
Here’s what that means in practice:
python
CopyEdit
x = [1, 2]
y = x
x.extend([3])
print(y) # [1, 2, 3]
Both x and y point to the same list. Changes to one affect the other. This is because lists in Python are mutable and stored by reference.
Also, keep in mind that extend() returns None:
python
CopyEdit
a = [1]
result = a.extend([2])
print(result) # None
Trying to store the return value will not give you a new list. It simply updates a.
Another internal detail is how Python handles memory. Lists are dynamic arrays. When you call extend(), Python might resize the list in memory to accommodate new items. This is done automatically and efficiently, so most users never notice. But in performance-critical tasks, using extend() is usually faster than list concatenation with +, which creates a new list every time.
The extend() method in Python is a simple yet useful way to grow your list with items from another iterable. It keeps your data flat, clean, and easy to work with. It doesn’t return anything because it changes the list directly. Whether you’re adding numbers, strings, or dictionary keys, extend() keeps things smooth by unpacking whatever you give it. Once you understand how it differs from append() and how it handles various inputs, you'll find it easy to use in everyday code. It's one of those tools that quietly makes your list handling much more efficient, saving time and avoiding common list structure mistakes in both small and large-scale Python projects.
Advertisement
Learn how to loop through dictionaries in Python with clean examples. Covers keys, values, items, sorted order, nesting, and more for efficient iteration
Discover different methods to check if an element exists in a list in Python. From simple techniques like using in to more advanced methods like binary search, explore all the ways to efficiently check membership in a Python list
Learn how to concatenate two or more DataFrames in pandas using concat(), append(), and keys. Covers row-wise, column-wise joins, index handling, and best practices
Learn 10 clean and effective ways to iterate over a list in Python. From simple loops to advanced tools like zip, map, and deque, this guide shows you all the options
Struggling with Copilot's cost or limits? Explore smarter alternative AI tools with your desired features and workflow.
Master the Python list insert() method with this easy-to-follow guide. Learn how to insert element in list at specific positions using simple examples
Discover Oracle’s GenAI: built-in LLMs, data privacy guarantees, seamless Fusion Cloud integration, and more.
Learn how __init__ in Python works to initialize objects during class creation. This guide explains how the Python class constructor sets instance variables, handles defaults, and simplifies object setup
Explore how developers utilize the OpenAI GPT Store to build, share, and showcase their powerful custom GPT-based apps.
Salesforce brings generative AI to the Financial Services Cloud, transforming banking with smarter automation and client insights
Think generative AI risks are under control? Learn why security issues tied to AI models are growing fast—and why current defenses might not be enough
Need to share a ChatGPT chat? Whether it’s for work, fun, or team use, here are 7 simple ways to copy, link, or export your conversation clearly