Python List Methods: .append() Vs .extend()
Tags: python, beginners, codenewbie, tutorial
Published: 2020-04-17 16:09:35

Append and extend are both Python methods used to add elements at the end of lists, but how do they differ and which one should you use?


##.append()

Append takes in one argument and adds it to the end of the list.

For example:
```python
snacks_for_later = ['chocolate', 'chips', 'almonds']
snacks_for_later.append('apples')
print(snacks_for_later) 
```
This will print out `['chocolate', 'chips', 'almonds', 'apples']`

Let’s say you have a lot of extra snacks you want to add to your list, you could do it like this:
```python
snacks_for_later.append('apples')
snacks_for_later.append('blueberries')
snacks_for_later.append('oranges')
```

But what happens if you don't want to keep typing the same command over and over again and try to append all the extra snacks at once? If we do this using append we will get an undesirable outcome:

```python
snacks_for_later = ['chocolate', 'chips', 'almonds']
snacks_for_later.append(['apples', 'blueberries', 'oranges'])
print(snacks_for_later)
```
This will print out `['chocolate', 'chips', 'almonds', ['apples', 'blueberries', 'oranges']]`

It added the elements to the list but it added the whole list of new snacks at the same index. `print(snacks_for_later[4])` will result in `['apples', 'blueberries', 'oranges']`.

We can sort this using .extend()

##.extend()
Extends allows you to add multiple elements to a list at once.

For example:
```python
snacks_for_later = ['chocolate', 'chips', 'almonds']
snacks_for_later.extend (['apples', 'blueberries','oranges'])
print(snacks_for_later)
```
Will print out `['chocolate', 'chips', 'almonds', 'apples', 'blueberries','oranges']`

Under the hood, extend iterates over its argument, adding the argument's elements to the initial list.

####A note of caution:
If you pass a string to extend it will iterate over the characters in the string and add them to the list separately:

```python
snacks_for_later = ['chocolate', 'chips', 'almonds']
snacks_for_later.extend ('apples')
print(snacks_for_later)
```
Will print out `['chocolate', 'chips', 'almonds', 'a','p','p','l','e','s']`

A similar thing would happen if you pass in a dictionary:
```python
snacks_for_later = ['chocolate', 'chips', 'almonds']
snacks_for_later.extend ({'snack1':'apples', 'snack2':'blueberries'})
print(snacks_for_later)
```
Will print out `['chocolate', 'chips', 'almonds', 'snack1', snack2']`.


##Takeaways:
* Both append and extend only take in one argument, trying to pass them more than one will result in a TypeError
* When you use append, the list's length grows by one. If you pass it a list or dictionary then the last element of the list will be that list or dictionary
* When you use extend, the list's length grows by the number of elements of extend's argument. If you pass it a list with length 3, all 3 elements will get added to the list separately and the list length will grow by 3.
* If you pass a string as extend's arguments every character from that string will get added to the list separately
* If you pass a dictionary to extend, it will add to the list just the keys of the dictionary