Last modified: Feb 13, 2025 By Alexander Williams
Python List Append vs Extend: A Beginner's Guide
Python lists are versatile data structures. Two common methods for adding elements are append
and extend
. Understanding their differences is crucial.
Table Of Contents
What is append?
Append adds a single item to the end of a list. It treats the argument as one element regardless of its type.
# Example of append
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
my_list.append([5, 6])
print(my_list) # Output: [1, 2, 3, 4, [5, 6]]
[1, 2, 3, 4]
[1, 2, 3, 4, [5, 6]]
What is extend?
Extend adds multiple items from an iterable to the end of a list. Each item becomes a separate element in the list.
# Example of extend
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]
my_list.extend('abc')
print(my_list) # Output: [1, 2, 3, 4, 5, 'a', 'b', 'c']
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 'a', 'b', 'c']
Key Differences Between Append and Extend
The main difference lies in how they handle arguments. append
treats the argument as one entity. extend
iterates through it, adding each item individually.
Use append
for single items. Use extend
for multiple items.
When to Use Append
Append
is ideal for adding one item or a complex object like another list or dictionary.
For example, appending dictionaries works well.
# Appending dictionaries
data = []
data.append({'name': 'Alice', 'age': 25})
data.append({'name': 'Bob', 'age': 30})
print(data) # Output: [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
When to Use Extend
Extend
is useful when merging two lists or adding multiple elements at once.
Check our guide on converting lists to dictionaries for advanced use cases.
# Extending lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # Output: [1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
Common Mistakes
A common mistake is using append
instead of extend
when merging lists.
This results in nested lists rather than a combined one.
# Incorrect usage of append
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.append(list2)
print(list1) # Output: [1, 2, 3, [4, 5, 6]]
[1, 2, 3, [4, 5, 6]]
Performance Considerations
Extend
can be faster for large datasets since it processes all items in one operation. However, the difference is negligible for small lists.
For performance tips, check out our article on removing elements efficiently.
Practical Example
Imagine collecting user input into a list. If each input is a single value, use append
. If inputs are lists themselves, use extend
.
# Collecting user input
user_input = []
user_input.append('apple') # Single item
user_input.extend(['banana', 'cherry']) # Multiple items
print(user_input) # Output: ['apple', 'banana', 'cherry']
['apple', 'banana', 'cherry']
Conclusion
In summary, append
and extend
serve different purposes in Python lists. Choose the right method based on your needs. For more Python tips, explore our guide on adding elements to the left of a list.