What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python List extend() Method ( with Examples and Codes )

  • Nov 29, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abrar Ahmed
Python List extend() Method ( with Examples and Codes )

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:

#list method
lis1 = list(12,14,15)
#using square braces
lis2 = [12,14,15]
#empty list
lis3 = []


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:

lis1 = [25,14,20,30]
adder = [12,10,18]
lis1.extend(adder)
print(lis1)

 

Output:

[25, 14, 20, 30, 12, 10, 18]


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;

lis1 = []
lis1.extend("Hello World")
print(lis1)

 

Output:

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

 

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:

lis1 = [25,14,20,30]
adder = {"key1":12, "key2":23,"key3":24}
lis2 = [25,28,29,30]
lis1.extend(adder.keys())
lis2.extend(adder.values())
print(lis1)
print(lis2)

 

Output:

[25, 14, 20, 30, 'key1', 'key2', 'key3']
[25, 28, 29, 30, 12, 23, 24]

 

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.

lis1 = [25,14,20,30]
adder = [12,10,18]
lis2 = [25,28,29,30]
lis1.extend(adder)
lis2.append(adder)
print(lis1)
print(lis2)

 

Output:

[25, 14, 20, 30, 12, 10, 18]
[25, 28, 29, 30, [12, 10, 18]]

 

Conclusion

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.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abrar Ahmed
An ambivert individual with a thirst for knowledge and passion to achieve. Striving to connect Artificial Intelligence in all aspects of life. I am also an avid coder and partake in coding challenges all the time on Leetcode and CodeChef.