Python provides a data structure that can store n number of elements all under the name of one variable. This data structure is called a list. In Python, the extend() method is a powerful tool for adding elements to a list. Whether you want to combine multiple lists, append the contents of an iterable, or expand a list with individual elements, extend() has got you covered. In this article, we will learn about Python List Extend, when to use this method, and how it works with code. But first, let's revise lists.
What are Lists?
Lists are a collection of items in python. They are used to store heterogeneous items, that is, items of multiple types, all under a common variable name. Lists are created in python using the in-built list() function or by specifying the items of the list within square braces. You can check out list functions in detail.
Difficult to catch? Have a look at the code example below to understand the different types of lists:
Now that we know how lists are created, let us talk about a few functionalities that list offer. Lists come with very powerful inbuilt methods that can help you modify and use the list as you want.
What is extend() method in the Python list?
One of the most powerful functions in lists is the extend method. Lists are mutable in nature. That means a list can be edited even after its creation. In order to edit a list, we can make use of built-in methods like the extend method. The extend method is used when we want to add multiple items to the list.
How do we use it? Have a look at the code below:
The extend method can be used to add any number of elements to the end of the list. They can be used to iterate over a tuple, a set, or even a dictionary to add elements to an existing list. The extend method can only be used to enter elements at the end of the list.
Another property of the extend method is that it does not return a value. What does that mean? It means that any changes you make to a list using the extend method, will not return a new list but instead will store the changes in the existing list itself.
The extend method works by going through an entire iterable, then adding each element in the iterable one by one to the existing list.
When do we use extend() in Python?
The most popular use case of the python extends method is to add new elements to a list. The python extend method can also be used with strings to split them into characters and store them inside the list.
To understand this use case, have a look at the code below;
What are we doing here? Strings in python are treated as a list of characters. Therefore when we use the extend method and pass a string as a parameter, the interpreter treats the string as a list of individual characters.
Then it adds each character that it encounters in the string one by one into the list, creating a list of separated characters from the string.
The extend method can also be used to add dictionary items to the list. Dictionaries are a collection of key and value pairs. These dictionary items that can be added to a list can be either the keys of the dictionary or the values of the dictionary.
How do we add them, have a look at the code example provided below:
In the example, we used either the dict.keys() to add the keys or the dict.values() to add the values of the dictionary to the list.
Python Extend vs Python Append
The python extend method and the python append method both are used to add elements at the end of the list. The fundamental difference between the two is that the extend method can be used to add multiple items to the list at once with one function call, whereas the append method can only add one element to the list in one function call.
What happens if we pass another iterable, such as a list in the function calls? If an iterable is provided to an extend method call, then all the elements inside the iterable will be added to the list one by one, increasing the length of the list by the number of items in the list.
But in the append method call, the entire iterable will be appended at the end of the list, as an element, and the length of the list will only increase by one. You can see the code below to understand the difference between extend and append further.
The extend method is a very useful method in python that can be used to add multiple items into the list in one go. It is effective and time efficient making the hassle of adding elements into a list very easy. You can use it also to split a string and store its characters into a new list. The extend method does not return a new list but instead makes changes to the existing list.