2 reasons to use extend() instead of append() in Python
Tags: python, beginners, programming, codenewbie
Published: 2022-04-10 16:19:08

Whether you are a Python noob or an expert, you have surely heard of **list**. **list** is one of Python's built-in data types.

Creating an empty list in Python is very simple:

```python
my_list = []
```

Once you have created an empty list, you can add items to it.

Python offers two methods for adding items to a list: **append()** and **extend()**.

Understanding how these methods work is important to choose which one to use.

## append()

`append()` adds an item to the end of the list. The item can be any Python object such as a _number_ or a _string_.

The following example shows how to add the string `'hello'` to the end of the `my_list` list.

```python
my_list.append('hello')
```

After the `append()` operation, `my_list` will contain a single element:

```
['hello']
```

## extend()

`extend()` adds all the items of an iterable to the end of a list. An iterable is a Python object capable of returning its members one at a time. Examples of iterables are **lists**, **tuples** and **strings**.

The following example adds several items to the end of the `my_list` list:

```python
items = ['hello', 'world']
my_list.extend()
```

After the `extend()` operation `my_list` will contain two elements:

```
['hello', 'world']
```

## Difference between `append()` and `extend()`

The main difference between `append()` and `extend()` is the number of items that can be added to the list. `append()` adds only a single item to the list. Instead, `extend()` can add all the items of an iterable to the list.

To add many items to a list using `append()`, we can define a loop, as shown in the following example:

```python
items = ['hello', 'world']
for item in items:
    my_list.append(item)
```

The above `for` loop iterates on the list of items and adds all the items to `my_list` one by one. Basically for each iteration it adds an item of `items` to `my_list`.

## Are `append()` and `extend()` equivalent?

Not exactly. There are two differences:
1. `extend()` makes the code more readable;
2. `extend()` is more efficient than `append()` (i.e. it requires less time to add the items to the list).

## Why `extend()` is more efficient?

When we want to add items to the list, the Python interpreter needs to expand the list. In order to expand the list, it allocates new memory and copies the new items to the newly allocated memory. This is common to both `append()` and `extend()` operations.

What happens when we add several items to a list using `append()` in a loop?

```python
items = ['hello', 'world']
for item in items:
    my_list.append(item)
```

In the above example, the Python interpreter iterates on each element of the items list. In each iteration, it allocates new memory in `my_list`. The size of the allocated memory is such as to contain an element of `items`.

First iteration: it allocates memory to store the `'hello'` string and copies the `'hello'` string to the newly allocated memory.

Second iteration: it allocates memory to store the `'world'` string and copies the `'world'` string to the newly allocated memory.

In conclusion, `append()` performs two allocate operations and two copy operations.

Now let's see what happens when we use `extend()` to add all the items to `my_list`:

```python
items = ['hello', 'world']
my_list.extend(items)
```

In this case, the Python interpreter allocates new memory to store both `'hello'` and `'world'` strings in `my_list`.

So, `extend()` performs only one allocate operation.

## Conclusion

Adding many items to a list using `append()` in a loop can require many allocate operations. In order to mitigate this inefficiency, Python developers implemented a over-allocation, which reduces the number of allocate operations required to append elements to a list. Over-allocation is out of scope for this post.

Allocation operations can be very costly. 

`extend()` performs only one allocate operation, which results in an improved efficiency. Consequently, less time is required to add the items to the list.

**In conclusion, you should always use `extend()` to add a set of items to a list.**

That's all for this post. If you liked this post, follow me on [dev.to](https://dev.to/cscarpitta/) and Twitter ([@cscarpitta94](https://twitter.com/cscarpitta94)) to get notified when I publish new posts. :blush: