Updated April 19, 2023
Introduction to Python List extend
The following article provides an outline for Python List extend. Here, we are seeing another method of the list such as the extend() method. In Python, the list is a data structure where all the elements or items are an ordered sequence. There are many methods that are applied on the lists this is also one among the list. When the given list needs to be extended then we use this method called extend(). The extend() method is another way of appending the list to the previous original list. This method adds all the elements to the given original list. There are also other ways to extend the list by using append() function or using a “+” operator or even by using a slicing method also we can extend the given list.
Working of extend() Method of the List with Examples
The extend() method is specially used on lists where it is mostly used for extending the given list by adding the given new elements of the new list to the previous list. The new list is added to the end of the previous list or the list that needs to be extended.
Now let us see the syntax of extend() method with an example.
Syntax:
The syntax of extend() method of the list is as follows:
List.extend(any_iterable)
Parameters:
- Any_iterable: iterable means any data structure can be passed as the argument such as tuple, set, list, etc.
So the return value of this method is nothing that does not return anything instead it modifies the given original list by just adding the elements of the list passed as the argument. So the existing list increases the number of elements by adding the elements that are passed in the argument of the method.
Example:
Now let us see the simple example of the extend() method applied on the list.
Code:
print("The below program demonstrates extend() function:")
print("\n")
programs = ['Python', 'C', 'C++']
print("The first list for extending this list is as follows:")
print(programs)
print("\n")
Topics = ['List', 'Tuple', 'Dictionary']
print("The second list for adding this list to the orignal is as follows:")
print(Topics)
print("\n")
programs.extend(Topics)
print("The extended list after applying extend() function:")
print(programs)
Output:
In the above program, we can see that we have declared one list with name “programs” which has three elements in it, and the second list that needs to be added to the first list is declared with the name “Topics” which also has three elements in it. Then we have used extend() function where we are extending the first list “programs” by adding the second list “Topics” to it. Hence this is done by the statement “ programs.extend (Topics) ”. This program results in the modified first list which now contains six elements where we can see in that all the elements of the second list are added at the end of the first list. The result can be seen in the above screenshot.
So this method works like merging the lists to the given list. This method also helps to add tuple to the list obtaining one single list.
Let us see how this is done using the same example as above.
Example:
Code:
print("The below program demonstrates extend() function:")
print("\n")
programs = ['Python', 'C', 'C++']
print("The first list for extending this list is as follows:")
print(programs)
print("\n")
Topics = ('List', 'Tuple', 'Dictionary')
print("The tuple is declared for adding this list is as follows:")
print(Topics)
print("\n")
programs.extend(Topics)
print("The extended list after applying extend() function for adding tuple to list:")
print(programs)
Output:
In the above program, we can see we have declared one list and one tuple. The list name is “programs” and the tuple is declared with the name as “Topics”. Then we are applying the extend() function and this is done by the statement “ programs.extend(Topics) ” which will add the tuple “Topics” to the list “programs” to obtain the single list and the result can be seen in the above screenshot. In this, we can see that this method helps to extend only the list and can add iterable to the list by using this extend() function.
Similarly, we can also add the elements of sets to the list by doing the save as above two examples. We can also extend the list by adding the elements of the sets also. But the result will also be the same which will output the single list which contains all the elements of both lists and sets.
As extend() function converts any iterable to list hence when we try to add the elements of an iterable like tuple, sets, etc will be converted to list first when we use extend() function and then it adds all the elements of the iterable to the given list to obtain the single list.
In Python, the extend() method and append() method working are the same. But there are few differences in using these methods in Python. The main difference between these methods is that append() function adds all the elements of the list which is passed as an argument as a single element to the end of the given list, whereas the extend() function iterates over the passed argument to it where it adds each element to the given list.
Conclusion
In this article, we conclude that the extend() function of the list in Python is used to extend the list by adding all the elements of the list to the given list to obtain a single list. In this article, we saw a simple example on extend() function to add the elements of one list to another list. Then we saw another example of adding all the elements of tuples to another list given. Therefore this can be done on adding the elements of sets to the list. So all obtain a single list by using extend() function and in Python even append() function works similarly to extend() function and we have mentioned the difference between these functions in this article.
Recommended Articles
This is a guide to Python List extend. Here we discuss the introduction and working of extend() method of the list with examples. You may also have a look at the following articles to learn more –