Explain Codes LogoExplain Codes Logo

What is the difference between Python's list methods append and extend?

python
list-methods
append-vs-extend
python-lists
Nikita BarsukovbyNikita Barsukov·Mar 11, 2025
TLDR

append() adds an entire object at the end of the list, keeping its structure, while extend() flattens the added iterable, appending its individual elements.

Using append() "as is":

my_list = [1, 2, 3] my_list.append([4, 5]) # Adds as a single item # Result: [1, 2, 3, [4, 5]]

Using extend() for seamless integration:

my_list = [1, 2, 3] my_list.extend([4, 5]) # Adds elements individually # Result: [1, 2, 3, 4, 5]

In terms of efficiency, append() is a O(1) operation, consistently fast no matter the list size. However, extend() operates at O(k) complexity where k is the length of the iterable. Both processes tweak the original list directly and do not create a new one.

When to use append() versus extend()

append() for individual elements or nested lists

When you have just a single object or a nested list to add to your list, append() is your friend:

elements = ["elephant", "tiger"] elements.append("lion") # ["elephant", "tiger", "lion"] elements.append(["cub", "kitten"]) # This is not Noah's Ark, repeat, not Noah's Ark # ["elephant", "tiger", "lion", ["cub", "kitten"]]

This approach is particularly helpful when you need to maintain relationships among data points or handle hierarchical data.

extend() for merging iterables

Plan to merge a list with another iterable (list, tuple, set)? Use extend(). It is more efficient compared to manually appending each item via a loop:

digits = [1, 2, 3] digits.extend([4, 5]) # The list wants to be a sequence from 1 to 5. Let's help it! # [1, 2, 3, 4, 5] digits.extend((6, 7)) # And now it dreams bigger! # [1, 2, 3, 4, 5, 6, 7]

But watch out! extend() can be more performance-intensive than only adding a single item with append().

Coding with clarity: append() vs extend()

Selecting the right method makes your Python code more legible and aligns with the intent of your script. extend() is prone to unpacking the element leading to unexpected data structures when adding a single non-iterable element, while using append() to add multiple items would instead result in a nested list.

Compatibility with += operator

The += operator complements extend() nicely and can be used for the same purpose with a cleaner syntax:

numbers = [1, 2, 3] numbers += [4, 5] # The numbers want in on that sequence action! # Result: [1, 2, 3, 4, 5]

However, this may conceal the code's intention unlike .extend(), which clearly states it.

Beware: Extend with a string

Remember that extend() will treat a string as an iterable of characters:

letters = ['a', 'b'] letters.extend('cd') # Surprise! Addition of 'c' and 'd' :) # ['a', 'b', 'c', 'd']

This can lead to unexpected results if you intended the whole string as a single element.

Type Safety and Consistency

While append() is more forgiving and can take any object, with extend(), as each element of the iterable is added individually, make sure all the elements are compatible with your list's intended data type.