append vs extend in Python Lists: Explained with Examples!

In this article let us learn the difference between the append() method and the extend() method that comes with Python list class and learn when to use which one!

For those of you in a hurry here is the short version of the answer.

append vs extend: In a Nutshell

The following picture summarizes the similarities and differences between append and extend

As you can see, the main difference is while append() adds a single element, extend() adds a number of elements to the end of the list.

Don’t worry if the above picture does not make sense to you as that was targeted at more experienced programmers who just wanted to refresh their memories. The rest of this article is dedicated to those of you in their first steps of their journey to become a Python Craftsman.

By the time you reach the end of this article, I suggest you come back and take another look at this picture, I assure you it will make much more sense then!

append vs extend: A More Thorough Answer

I remember the time when I first came across lists and its methods. To add elements it has insert, append and extend methods. The insert method is fairly straight forward, but append and extend methods confused me for quite sometime, as

  1. They both sound very similar
  2. They both add items to the end of the list

If you feel like you could take a refresher on Python lists, I suggest reading the article below.

Python Lists: Everything You Need To Know!

Coming back to the topic, lets first take a look at the append method.

The append() Method

The meaning of the word append is “to add (something) to the end of (some other thing)”. This is what exactly our append method does as well, it takes in an item you enter and adds it to the end of the list of your choice.

For example:

alphabets = ["a","b","c","d"]
print("The list before we use the append method:", alphabets)
alphabets.append("e")
print("The list after we use the append method: ", alphabets)

OUTPUT:

The list before we use the append method: ['a', 'b', 'c', 'd']
The list after we use the append method: ['a', 'b', 'c', 'd', 'e']

You should also know that since the size of the list we’re appending to increases by only one, appending a list of items might not go like how you thought it would.

alphabets = ["a","b","c","d"]
print("The list before we use the append method:", alphabets)
alphabets.append(["e","f","g"])
print("The list after we use the append method: ", alphabets)

OUTPUT:

The list before we use the append method: ['a', 'b', 'c', 'd']
The list after we use the append method: ['a', 'b', 'c', 'd', ['e', 'f', 'g']]

Here the list [“e”,”f”,”g”] is treated as a single item and is added to the end of the list.

If you were looking to merge the two lists such as [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’], this is where the extend() method comes in!

The extend() Method

The extend method is similar to the append method, but cooler. The only difference is that in the extend function, we are able to add multiple elements in one go!

How this works is, this method takes in one item just like append but this item must be an “iterable collection of items“. Confused? Let me explain this using the same example as earlier, but with the extend() method this time:

alphabets = ["a","b","c","d"]
print("The list before we use the extend method:", alphabets)
alphabets.extend("e","f","g")
print("The list after we use the extend method: ", alphabets)

OUTPUT:

The list before we use the extend method: ['a', 'b', 'c', 'd']
The list after we use the extend method: ['a', 'b', 'c', 'd', 'e', 'f', 'g']

As you can see, the second list is not merged with the 1st one.

append() vs extend()

Let’s now look at 3 carefully chosen examples to learn the differences between append and extend methods.

Example#1

numbers = [1,2,3,4]
print("The list before we use the append method:", numbers)
numbers.append(5)
print("The list after we use the append method: ", numbers)

OUTPUT:

The list before we use the append method: [1, 2, 3, 4]
The list after we use the append method: [1, 2, 3, 4, 5]

Let’s do the same thing with the extend method now:

numbers = [1,2,3,4]
print("The list before we use the extend method:", numbers)
numbers.extend([5,6,7,8,9])
print("The list after we use the extend method: ", numbers)

OUTPUT:

The list before we use the extend method: [1, 2, 3, 4]
The list after we use the extend method: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Remember, since the extend method by default is designed to take in an iterable value, we can pass that only. Hence we passed a list for our example, we can do the same thing with different iterables too. For example, using a tuple or a set is valid and achieves the same thing:

#tuple
alphabets.extend((5,6,7,8,9))

#set
alphabets.extend({5,6,7,8,9})

Example#2

I have a list list1 where its only current element is a string named ‘hello’, let me complete it by adding another string ‘world’. I’ll use both methods to show the differences:

list1 = ["hello"]

print("The list before we use the append method:", list1)

list1.append("world")
print("The list before we use the append method:", list1)

OUTPUT

The list before we use the append method: ['hello']
The list before we use the append method: ['hello', 'world']

As you can see, the append method simply regards the inputted string as one element and appends it to our list. Now let’s try the same with the extend method:

list1 = ["hello"]

print("The list before we use the extend method:", list1)

list1.extend("world")
print("The list before we use the extend method:", list1)

OUTPUT

The list before we use the extend method: ['hello']
The list before we use the extend method: ['hello', 'w', 'o', 'r', 'l', 'd']

The extend method accepts one iterable item as input. In this case, as the string is iterable, the extend method looped through it letter by letter and adding each of them as an element to our original list.

If you try to give an item which is not iterable, then your code will crash with a TypeError as shown in the example below.

>>> numbers = [1, 2, 3]
>>> numbers.extend(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

As you can see we tried to give 4 as the argument to the extend method and that resulted in an error with the last line saying ‘int’ object is not iterable.

If you wish to read these error messages like a pro we have an excellent article on that topic which you can find in the link below.

Python: Details contained in an Exception

It is important to keep in mind the differences between these two and to know when to use them appropriately. Otherwise, you might end up mixing these two or using them in scenarios where they are not suitable at all.

Lets take a look at another example just to cement the concept in our heads

Example#3

Let’s say we had two lists list1 and list2

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]

If you wanted to combine them in numerical order, you’ll have to add the elements of list2 to the end of list1.

Guess what happens if we use the append method?

list1.append(list2)
print(list1)

OUTPUT:

[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]

We have list2 inside list1. This isn’t what we wanted, so let’s try with the extend method now:

list1.extend(list2)
print(list1)

OUTPUT:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

which is exactly the output we were looking for!

Implementing extend using append

We can do the same thing extend does using append and a loop. Have a look at the following code:

shapes_1 = ["circle", "triangle", "square"]
shapes_2 = ["rectangle", "oval", "star"]

for shape in shapes_2:
    shapes_1.append(shape)
    
print(shapes_1)

OUTPUT:

['circle', 'triangle', 'square', 'rectangle', 'oval', 'star']

This approach is useful in several scenarios, for example, let’s say you wanted to add individual elements based on a condition, you wouldn’t be able to do with the extend method as it adds elements from an iterable data structure as a whole.

For example let’s say you had three lists, each of different ranges and sizes. If you wanted to make a separate list containing only the even numbers of these three lists, here’s how you could do it:

a = [-4,-3,-2,-1,0,1,2,3,4,5]
b = [117,118,119,120]
c = [77,78,79]

even_list = []

for current_list in [a,b,c]:
    for number in current_list:
        if number % 2 == 0:
            even_list.append(number)
        
print(even_list)

OUTPUT

[-4, -2, 0, 2, 4, 118, 120, 78]

And with that example, I will end this article.

Congratulations for making it till the end of the article, not many have the perseverance to do so!

I hope you enjoyed reading this article and found it useful!

Feel free to share it with your friends and colleagues!

If your thirst for knowledge have not been quenched yet, here are some related articles that might spark your interest!

Related Articles

Python Dictionary: Everything You Need To Know!

Python Lists: Everything You Need To Know!

Python Sets: Everything you need to know!

Python: Details contained in an Exception

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Editor
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn