Python append() and extend() List Methods

In this article we are going to take a look at two important Python list methodsappend() and extend(). Both are very useful, but each serves a different purpose.

Python List Addition, AKA Concatenation

Have you ever wondered how to add elements to a list in Python? You might think that since Python is so intuitive, maybe the + operator would do the trick. Open up a Python shell (for example the one you get when you first open Python IDLE), and try for your self.

>>> my_list = [1, 2, 3]
>>> my_list = my_list + 4

When you try this, you get an error: TypeError: can only concatenate list (not "int") to list

Concatenation means “joining”, which is what we were hoping to do using the + operator. However, my_list is a of data type list and 4 is of type int, and Python doesn’t like it!

So what do we do instead?

>>> my_list = [1, 2, 3]
>>> my_list = my_list + [4]
>>> print(my_list)
[1, 2, 3, 4]

Now Python is happy and the new element has been added to the end of the list. Some of you may be a little confused as to why we get the answer we do. After all, wouldn’t [1, 2, 3, [4]] make more sense, as we’ve “added” [4] to [1, 2, 3]? You may or not think so, but the bottom line is “that’s the way Python does it,” and in fact it’s very useful that it does, as you will see when you gain some experience with this kind of list operation.

The Python append() List Method

Another way to achieve the same result as above is this:

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

Note that the append() method only takes ONE argument, and you will get an error if you supply more.

So what about appending multiple values?

You might think this would work:

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

However, it doesn’t do what you might expect:

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

The list itself supplied as the argument to append() had been appended!

The Python extend() Method

One way you could get around this problem would be like so:

>>> my_list = [1, 2, 3]
>>> my_extra_items = [4, 5]
>>> for item in my_extra_items:
...     my_list.append(item)
...
>>> my_list
[1, 2, 3, 4, 5]

This gives us the desired result, but is a little cumbersome. A more elegant solution is to use the Python extend() list method, like so:

>>> my_list = [1, 2, 3]
>>> my_list.extend([4, 5])
>>> my_list
[1, 2, 3, 4, 5]

Neat isn’t it? It may take a little practice to get confident with how both the append() and extend() methods work, but it’s worth the effort to gain this kind of mastery over Python lists, which are so fundamental to the language.

The key question to ask yourself is: do I want to add a new list containing these elements to my list, or do I want to just add the elements of this list to my list?

Practice with the Python append() and extend() List Methods

To give you some practice with the Python append() and extend() list methods, try to predict the output of running the code below. There may be a few suprises!

res = []
res.append("A")
print(res)

res = []
res.append(["A"])
print(res)

res = []
res.extend(["A"])
print(res)

res = []
res.append(["A", "B"])
print(res)

res = []
res.extend(["A", "B"])
print(res)

res = [[]]
res.append("A")
print(res)

res = [[]]
res.append(["A", "B"])
print(res)

res = [[]]
res.extend("A")
print(res)

res = [[]]
res.extend(["A", "B"])
print(res)

That it for now. I hope you found this article on the Python append() and extend() list methods helpful.

Happy computing!

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *