Understanding Python’s extend() Method for Lists

Advertisement

May 10, 2025 By Alison Perry

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.

What is the extend() Method in Python?

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.

How extend() Works: Syntax and Basic Examples

This method is designed for simplicity. You call it on a list, pass an iterable, and your list grows.

Combining Lists

python

CopyEdit

a = [10, 20]

b = [30, 40]

a.extend(b)

print(a) # [10, 20, 30, 40]

Adding Characters from a String

python

CopyEdit

letters = ['x']

letters.extend('yz')

print(letters) # ['x', 'y', 'z']

Extending with a Set

python

CopyEdit

items = [1]

items.extend({2, 3})

print(items) # [1, 2, 3] (order may vary)

Using a Dictionary

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.

Nested Data with extend()

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.

Working with an Empty List

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.

Inside a Loop

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.

Key Differences Between extend() and append()

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.

How extend() Affects List Behavior Internally?

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.

Conclusion

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

Recommended Updates

Technologies

How to Iterate Over a Dictionary in Python?

Tessa Rodriguez / May 11, 2025

Learn how to loop through dictionaries in Python with clean examples. Covers keys, values, items, sorted order, nesting, and more for efficient iteration

Technologies

Efficient Ways to Check for Element Existence in a Python List

Alison Perry / May 09, 2025

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

Technologies

10 Easy Ways to Concatenate DataFrames in Pandas

Alison Perry / May 11, 2025

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

Technologies

How to Loop Through Lists in Python: 10 Useful Techniques

Alison Perry / May 11, 2025

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

Technologies

Is It Time to Switch from Microsoft 365 Copilot?

Alison Perry / May 26, 2025

Struggling with Copilot's cost or limits? Explore smarter alternative AI tools with your desired features and workflow.

Technologies

Inserting Items into Lists in Python: How the insert() Method Works

Tessa Rodriguez / May 08, 2025

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

Technologies

9 Things To Know About Oracle's Generative AI Service

Tessa Rodriguez / May 26, 2025

Discover Oracle’s GenAI: built-in LLMs, data privacy guarantees, seamless Fusion Cloud integration, and more.

Technologies

Understanding __init__ in Python: A Beginner’s Guide to Class Setup

Alison Perry / May 10, 2025

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

Technologies

How the OpenAI GPT Store Helps Developers Share Custom GPTs

Tessa Rodriguez / May 27, 2025

Explore how developers utilize the OpenAI GPT Store to build, share, and showcase their powerful custom GPT-based apps.

Technologies

How Is GenAI Transforming Salesforce’s Financial Services Cloud for Banking?

Alison Perry / May 22, 2025

Salesforce brings generative AI to the Financial Services Cloud, transforming banking with smarter automation and client insights

Technologies

8 Reasons Generative AI Security Problems Are Escalating

Tessa Rodriguez / May 20, 2025

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

Technologies

Best Ways to Share Your ChatGPT Conversations Easily

Alison Perry / May 20, 2025

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