Software, Tech & Coding simplified.

Python append() vs extend(): What Is the Difference

append vs extend in Python
append() vs extend() in a nutshell

In Python, the difference between the append() and extend() method is that:

  • The append() method adds a single element to the end of a list.
  • The extend() method adds multiple items.

Here is a quick cheat sheet that describes the differences.

extend()append()
Adds multiple elements to a list.Adds the input as a single element to a list.
Length increases by the number of elements added.Length increases by 1.
Time complexity is O(n), with n being a number of list elements.Time complexity O(1) or constant.

In this comprehensive guide, you are going to take a detailed look at the differences between append() and extend().

append() vs extend(): Overview

In Python, a list is a mutable data type used for storing elements. Lists support easy access to the elements. You can add, update, and delete items.

In Python, you need to deal with lists a lot.

When working with a list you commonly want to add elements to it after creation.

For example, if your list represents a queue for a lunch, you want to be able to add/remove participants at any time.

In this guide, we are going to focus on two ways of adding elements to the end (right-hand side) of a list:

  • The append() method.
  • The extend() method.

These methods are built-in into the list data type.

Let’s start adding elements to a list with the append() method.

append()

A Python list type supports the append() method. This method adds an element to the end (right-hand side) of an existing list.

The syntax for the append method is:

list.append(element)

Where:

  • list is the existing list to which we want to add an element.
  • element is the element to be added to the end of list.

This method only accepts one argument at a time. In other words, you cannot add multiple elements at the same time.

Perhaps the most common way to use the append() method is by adding a single value to the end of a list.

Example 1

For example, let’s add a single number to the end of a list of numbers:

a = [1, 2, 3]
a.append(4)

print(a)

Output:

[1, 2, 3, 4]

As you can see, the original list of numbers was updated and now there is an extra number at the end of it.

Example 2

As another example, let’s try to add multiple numbers into the list of numbers using the append() method.

a = [1, 2, 3]
a.append(4, 5)

print(a)

As mentioned, the append() method does not take more than one argument. Thus, doing this results in an error:

TypeError: append() takes exactly one argument (2 given)

At this point, you might consider a workaround of adding multiple numbers as a list.

a = [1, 2, 3]
a.append([4, 5])

print(a)

Output:

[1, 2, 3, [4, 5]]

But as you can see, this does not work as you would like it to.

Instead of adding the elements separately, the append() method just adds the whole list to the end of the other list as a single element.

So now the list of numbers consists of three numbers and one list of numbers.

append method in Python
The append() method treats a list of numbers as a single object.

If you want to add multiple elements to a list using the append() method, you need to create a loop.

Example 3

Let’s add multiple elements to the end of a list with a for loop and the append() method:

a = [1, 2, 3]

for number in [4, 5]:
    a.append(number)

print(a)

Output:

[1, 2, 3, 4, 5]

Now the numbers are added to the end of the list the way you wanted.

However, because adding multiple elements in one go is such a common thing to do, there is a separate method for doing that.

To add multiple elements to the end of a list, use the extend() method.

extend()

In Python, you can use the extend() method to add multiple elements to the end of a list. Similar to the append() method, the extend() method modifies the original list.

The syntax of the extend() method is:

list.extend(elements)

Where:

  • list is the name of the list you want to add elements to.
  • elements is an iterable, such as a list, that has the elements you want to add to the end of list.

Behind the scenes, the extend() method loops through the elements provided as arguments and adds them to the end of the list.

A common use case for the extend() method is to add a number of elements to an existing list.

Example 1

For example:

a = [1, 2, 3]

a.extend([4, 5])

print(a)

Output:

[1, 2, 3, 4, 5]

Notice how this is much neater than using a for loop + the append() method you saw in the previous chapter.

extend method in Python

By the way, the extend() function argument does not need to be a list. As I mentioned, it can be any iterable type in Python.

In Python, strings and tuples are also iterables.

If you are unfamiliar with iterables, please read this article.

Anyway, let’s see some examples of using other types of iterables with the extend() method.

Example 2

For instance, instead of specifying the numbers as a list, you could use a tuple:

a = [1, 2, 3]

a.extend((4, 5))

print(a)

Output:

[1, 2, 3, 4, 5]

Example 3

As another example, let’s pass a string argument to the extend() method:

a = [1, 2, 3]

a.extend("45")

print(a)

Output:

[1, 2, 3, '4', '5']

This works because a string is iterable. In other words, one string is a sequence of characters.

In the above code, the extend() method loops through the string character by character and adds each character to the end of the list.

Notice that you cannot extend a single element with the extend() method. This is because the type of argument needs to be iterable, that is, something that can be looped through

Example 4

For example, an integer cannot be looped through. Thus, adding a single integer using the extend() method fails.

Here is an illustration:

To add a single element with the extend() method, you need to place the element into an iterable.

Example 5

Let’s use the extend() method to add a single element to the end of a list. To do this, the element needs to be wrapped inside of an iterable.

a = [1, 2, 3]

a.extend([4])

print(a)

Output:

[1, 2, 3, 4]

However, instead of doing it this way, use the append() method as it is semantically more appropriate.

Recap

At this point, you should have a good understanding of how the append() and extend() methods work in Python.

To recap, the difference between append() and extend() methods is:

  • The append() method adds a single element to a list.
  • The extend() method adds multiple elements to a list.

Next, let’s take a look at the performance of these methods by running simple experiments.

Performance: append() vs extend()

Based on the previous example, you can do the same things using append() and extend().

To add multiple elements, you can use:

  • extend() by giving it an iterable argument.
  • append() by looping through an iterable and appending each element separately.

Likewise, to add a single element to the end of a list, you can use:

  • append() method by passing the single element as an argument.
  • extend() method by wrapping the single element into an iterable and passing it as an argument.

This might make you wonder how these approaches compare to one another when it comes to performance.

Let’s compare the performances of append() and extend() methods by running simple experiments.

Adding Multiple Elements: append() vs extend()

Let’s run an experiment of adding multiple elements to a list.

We are going to compare append() with for loop approach to extend() method.

Here is the code:

import timeit

def append(l, iterable):
    for item in iterable:
        l.append(item)
        
def extend(l, iterable):
    l.extend(iterable)
    
t_append = min(timeit.repeat(lambda: append([], "0123456789876543210123456789"), number=100_000))

t_extend = min(timeit.repeat(lambda: extend([], "0123456789876543210123456789"), number=100_000))

print(f"Appending took {t_append}s\nExtending took {t_extend}s")

Output:

Appending took 0.16888665399892488s
Extending took 0.034480054999221466s

As you can see, appending took ~5 times longer than extending.

So performance-wise, use the extend() method when adding multiple elements to a list.

Adding a Single Element: append() vs extend()

As another example, let’s compare the performance of adding a single element to the end of a list by:

  • Using the append() method by passing a single element as an argument.
  • Using the extend() method by wrapping the element into an iterable.

Here is the code:

import timeit

def append_one(l, elem):
    l.append(elem)
        
def extend_one(l, elem):
    l.extend([elem])

t_append = min(timeit.repeat(lambda: append_one([], 0), number=2_000_000))

t_extend = min(timeit.repeat(lambda: extend_one([], 0), number=2_000_000))

print(f"Appending took {t_append}s\nExtending took {t_extend}s")

Output:

Appending took 0.38043899900003453s
Extending took 0.49051314600001206s

As you can see, extending wastes a bit more time than appending.

However, you should not pay too much attention to the timings of these approaches. Instead, you should use an approach that is semantically correct. In other words, instead of looking at the performance numbers, use code that accurately describes what you are trying to accomplish.

Anyway, this completes our guide on append() vs extend() in Python.

To finish off, let’s take a look at two alternative approaches to adding elements to lists.

Other Similar Methods

In Python, append() and extend() methods are not the only ways to add elements to the end of a list.

In addition to these, there are two other ways you can use:

  • The insert() method.
  • The + operator.

Let’s briefly introduce these methods.

insert()

In Python, the insert() method can be used to insert an element at a specific index in a list.

The syntax is as follows:

list.insert(index, element)

Where:

  • index is the index at which you want to add the element.
  • element is the element to be inserted in the list.

This means you can use insert() to add an element to the end of a list too.

For instance, let’s insert a number at the end of a list:

a = [1, 2, 3]

a.insert(3, 4)

print(a)

Output:

[1, 2, 3, 4]

This method only allows you to insert a single element at a time.

If you try to add multiple elements as a list to the end of a list, you get a similar result when using append() this way.

For instance:

a = [1, 2, 3]

a.insert(3, [4, 5, 6])

print(a)

Output:

[1, 2, 3, [4, 5, 6]]

The + Operator

In Python, you can also add multiple elements to the end of a list using the addition operator (+).

Notice that this does not modify the original list. Instead, it creates a new list with the added elements.

For example:

a = [1, 2, 3]

a = a + [4, 5]

print(a)

Output:

[1, 2, 3, 4, 5]

Conclusion

Today you learned what is the difference between the append() and extend() methods in Python lists.

To recap, both methods can be used to add elements to the end of a list.

  • The append() can be used to add one element at a time.
  • The extend() method can be used to add multiple elements at once.

Thanks for reading.

Happy coding!

Further Reading

Best Programming Sites

50 Python Interview Questions

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email