Print("The tuple is declared for adding this list is as follows:") Let us see how this is done using the same example as above. This method also helps to add tuple to the list obtaining one single list. So this method works like merging the lists to the given list. The result can be seen in the above screenshot. 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. Hence this is done by the statement “ programs.extend (Topics) ”. Then we have used extend() function where we are extending the first list “programs” by adding the second list “Topics” to it. 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. Print("The extended list after applying extend() function:") Print("The second list for adding this list to the orignal is as follows:") Print("The first list for extending this list is as follows:") Now let us see the simple example of the extend() method applied on the list.Ĭode: print("The below program demonstrates extend() function:") So the existing list increases the number of elements by adding the elements that are passed in the argument of the method. 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. Any_iterable: iterable means any data structure can be passed as the argument such as tuple, set, list, etc.The syntax of extend() method of the list is as follows: List.extend(any_iterable) Now let us see the syntax of extend() method with an example. The new list is added to the end of the previous list or the list that needs to be extended. 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. Working of extend() Method of the List with Examples 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. This method adds all the elements to the given original list. The extend() method is another way of appending the list to the previous original list. When the given list needs to be extended then we use this method called extend(). There are many methods that are applied on the lists this is also one among the list. In Python, the list is a data structure where all the elements or items are an ordered sequence. Here, we are seeing another method of the list such as the extend() method. The following article provides an outline for Python List extend.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |