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.

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?

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:

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:

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

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:

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:

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!

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

Happy computing!

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python