In this article, we will learn the differences between append and extend list methods in Python. Each of these methods have their own advantages and disadvantages. So we will take a look at each of these methods and see when to use which.
Sounds good? Great! Then let us get started.
To begin with, let me tell you the environment I am using to run my Python code. I am using Python 3 installation running on a Ubuntu 16.04 OS. But even if you have a different set up, I think this code should still run fine for you as well.
Alright, so with that out of the way, let us start looking at the actual code itself.
So to begin with, let us enter into the Python interpreter using the command:
Python 3 Python 3.6.9 (default, Jul 3 2019, 15:36:16) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
So with this, we are now in our Python interpreter. Which means, every instruction that I now type in the console above will be taken in as a Python command or input. Right?
So then, let us create two new Python lists here. We can do this by using the commands:
a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 10]
So now we have our two Python lists. It is time for us to now see how we can use the append() and extend() list methods to work on these two lists.
Python List append( ) method
So what does this list append( ) method do? Let us find out by running the below command:
>>> a.append(b) >>>
So what do you think happened here? We don’t see any output after running the above command. So does that mean the code had no impact at all? Or do you think list b gets appended to list a so that we have one giant list of both a and b list elements?
Take a minute to think over before continuing further.
So to understand what happened, let us see what the value of each of these lists are. Here is the list outputs I see now:
>>> b [6, 7, 8, 9, 10] >>> a [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]] >>>
So as you can see above, list b is still the same. But list a has now changed. But the content of list a is not what we expected!
So it looks like what happened here is that append( ) method simply inserted the entire list b as a new list element to list a!
Woah! Not the output we were expecting. Right? But that is what Python list’s append( ) method does. It simply adds it’s arguments as a new list element to it’s parent list!
Python List extend( ) method
So now that we know what list append( ) does, it is now time for us to see what extend( ) method will do. So let us restart our Python3 interpreter and add these two list once again:
python3 Python 3.6.9 (default, Jul 3 2019, 15:36:16) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> a = [1, 2, 3, 4, 5] >>> b = [6, 7, 8, 9, 10] >>>
But this time, we will call the Python list’s extend( ) method.
>>> a.extend(b) >>>
Once again, it appears like nothing much changed. But we know that one of the list could be altered like in the case of append method right. So let us check what the content of list b & a is right now.
>>> b [6, 7, 8, 9, 10] >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Aha! As you can see now, list b still remains the same, but list a has the the individual elements of list b appended to it! So in other words, we have managed to “extend” list a to include content of list b.
Now this is exactly what we wanted! Right?
So with this understanding, we can now easily say what the difference between the two list functions are.
Difference between Python list append and extend methods
When we use Python’s append( ) method on a list, the parameter passed through this method will be appended as one new element to the parent list.
But on the other hand, when we use the extend( ) list method, the elements of the list passed as the parameter will be added individually to the parent list!
I think for the most use cases, we want the behavior of extend( ) method while working with lists. But there will be situations where the behavior of the append( ) list method is also preferred!
Hence, we have these two different Python List methods – append and extend!
I hope this was clear to you. But if you still have any questions or comments, do let me know in the comment section below. I would love to hear about it.
So with this, I will end my article here. Until next time, take care!
Want to know how to check for a substring using Python? Check this out!