list.extend()
Adds all items from an iterable to the end of a list (mutates).
my_list.extend(iterable)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Adds all items from an iterable to the end of a list (mutates).
The extend() method iterates over the specified iterable (such as a list, tuple, set, or string) and appends each element to the end of the current list. This operation modifies the list in-place and returns None, which is a common source of bugs for developers expecting a new list. In terms of performance, extend() is generally more efficient than repeated calls to append() because it allows the underlying array to resize more efficiently if the size of the iterable is known. The time complexity is O(k), where k is the length of the iterable being added. A notable edge case occurs when passing a string: since strings are iterables in Python, extend() will append each character individually rather than the whole string as a single unit.
Quick reference
Syntax
my_list.extend(iterable)
Inputs
Parameters
See it in practice
Examples
Merging Two Lists
fruits = ['apple', 'banana']
more_fruits = ['cherry', 'date']
fruits.extend(more_fruits)
print(fruits)['apple', 'banana', 'cherry', 'date']
Standard usage to flatten and add elements from one list to another.
Extending with a Tuple
numbers = [1, 2, 3]
coords = (4, 5)
numbers.extend(coords)
print(numbers)[1, 2, 3, 4, 5]
Demonstrates that extend() accepts any iterable, including tuples.
The String Iteration Behavior
tags = ['python', 'code']
tags.extend('web')
print(tags)['python', 'code', 'w', 'e', 'b']
Strings are iterables; extend() adds each character individually. Use append() if the whole string is needed.
Debug faster
Common Errors
TypeError
Cause: Passing a non-iterable object (like an integer or None) to the extend() method.
Fix: Ensure the argument is a list, tuple, or other iterable, or use append() for single non-iterable items.
items = [1, 2]
items.extend(3) # Raises TypeErrorLogicError
Cause: Assigning the result of extend() to a variable, expecting a new list.
Fix: Call extend() on its own line; it modifies the list in-place and returns None.
my_list = [1, 2]
new_list = my_list.extend([3, 4])
print(new_list) # Output: NoneRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Adds all items from an iterable to the end of a list (mutates).
iterable: Items to add (e.g., list, tuple, set).
TypeError: Ensure the argument is a list, tuple, or other iterable, or use append() for single non-iterable items. LogicError: Call extend() on its own line; it modifies the list in-place and returns None.