Two of the most common List methods in Python are the append and extend methods.
However, these two methods are the cause of a lot of confusion and misunderstanding among Python beginners.
In this article, I will explain what each method does and show you exactly the difference between them.
The append method is used to add an object to a list.
This object can be of any data type, a string, an integer, a boolean, or even another list.
Say you want to append an item to a list L>/span> that initially has 4 elements
>>> L = [1, 2, 3, 4] >>> L.append(5) >>> L [1, 2, 3, 4, 5]
As you can see, the append method adds the new item 5 to the list.
Needless to say, the length of the list has increased by one (and only one) because the append method adds only one object to the list.
This is an important distinction because you will see later that this is not necessarily the case with extend.
Alright, just out of curiosity let’s try to append a list to our list.
>>> L = [1, 2, 3, 4] >>> L.append([5, 6, 7]) >>> L [1, 2, 3, 4, [5, 6, 7]]
So what we did here is we appended one object (which happens to be of type list) to our list L
Again, after the modification the list length grew by only one.
Now let’s take a look at a similar, yet different, method.
extend is another very common list method.
Unlike append that can take an object of any type as an argument, extend can only take an iterable object as an argument.
An iterable object is an object that you can iterate through like strings, lists, tuples, dicts, or any object with the __iter__() method.
What extend does is very straightforward, it iterates through the iterable object one item at a time and appends each item to the list.
For example, let’s try to extend a list by another list.
>>> L = [1, 2, 3, 4] >>> L.extend([5, 6, 7]) >>> L [1, 2, 3, 4, 5, 6, 7]
As you can see in the example above, extend takes a list (which is an iterable) as an argument and appends each item of the list to L.
Three integer objects were appended to the list and the list size grew by three.
This behavior is obviously different from that of the append method.
Let’s look at another example with a different iterable object, strings.
>>> L = [1, 2, 3, 4] >>> L.extend("hi") >>> L [1, 2, 3, 4, 'h', 'i']
extend iterates through the characters of “hi” and appends each character to L.
Side Note: How is Extend different from the (+) Operator?
One question you might ask is how the extend method is different from using the + operator.
>>> L = [1, 2, 3, 4] >>> L + [5, 6, 7] [1, 2, 3, 4, 5, 6, 7]
This above code seems to have the same effect of extending a list by another list, right?
Well there are two major differences:
1. you can’t use the (+) operator to extend a list by any iterable other than a list
For example, you can’t use the (+) operator to extend a list with a string, or a tuple, or a dict.
2. extend modifies the list in place whereas the (+) operator creates a new list
>>> L = [1, 2, 3, 4] >>> L + [5, 6] [1, 2, 3, 4, 5, 6] >>> L [1, 2, 3, 4]
In the previous example, notice that L has not changed at all.
This is because the plus operator creates a new list object that holds the concatenated list.
To use the resulting list, you might need to store it in another variable first.
>>> c = L + [5, 6] >>> c [1, 2, 3, 4, 5, 6]
Read: Python Lists vs Tuples
1- Both extend and append are list methods that are used to add items to a list.
2- append adds one object of any type to a list.
3- extend operates on iterable objects and appends every item in the iterable to the list.
4- using the (+) operator is not equivalent to using the extend method.
If you are a beginner, then I highly recommend this book.
No longer a beginner?
Then it’s time to take your Python skills to the next level with this book (It’s my favorite).
Are you Beginning your Programming Career?
I provide my best content for beginners in the newsletter.
- What programming language to start with?
- Do you need a CS degree to be a programmer?
- Career tips and advice
- Programming tutorials
And so much more…