Introduction to Python List extend
This post will offer you with a framework for using Python List extend. The list’s extend() function is only one example of its many available methods. The list is a fundamental data structure in Python, and its elements always form a sequential list. This is only one of several techniques used to organize list data. The extend method is used to add items to the supplied list (). To add to an existing list, you may use the extend() function. This procedure appends all items to the original list provided. The append() function, the “+” operator, and the slicing method are all additional means by which the provided list can be expanded.
list.
Working of extend() Method of the List with Examples
When working with lists, the extend() method is typically used to add the new list’s elements to the existing list, expanding it. The enlarged or prior list is appended to the end of the new one.
Let’s have a look at an example of the syntax for the extend() function. Syntax:
This is the syntax for the list’s extend() method:
follows:
Parameters List.extend(any_iterable)
:
-
Any_iterable:
iterable means any data structure can be passed as the argument such as tuple, set, list, etc.
This function does not return a result, but rather alters the original list by adding the components of the list supplied as an input. The number of items in the list grows as a result, since the components supplied as the method’s parameter are appended to the existing list. Example:
This is a basic demonstration of the extend() function being used on a list. Code:
Product 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)
The preceding code demonstrates the declaration of two lists, one named “programs” with three items and the other named “Topics” with three items, both of which will be appended to the first list. The first list, “programs,” has been “extended” by adding the second list, “Topics,” using the extend() method. Hence, ” programs.extend (Topics ) ” is used to do this. The amended first list produced by the program now has six items, as can be seen by appending the contents of the second list to the end of the first. The outcome is seen in the above snapshot.
This procedure is analogous to merging the lists into the provided list. This procedure is also useful for merging many lists into one.
Let’s use the same example from up above to understand how this is accomplished. Here’s a Code:
Resulting 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)
It is clear from the preceding code that two data structures—a list and a tuple—have been defined. The stated name of the tuple is “Topics,” while the list’s name is “programs.” Finally, we use the extend() method, as seen in the above screenshot, by saying ” programs.extend(Topics) “, which combines the two lists into a single one by appending the tuple Topics to the list programs. This demonstrates how the extend() function can be used to expand the list and add iterable to it.
The elements of sets may be added to the list in the same way that the previous two instances were saved. The sets’ individual components may be added to the list as well. Nonetheless, the end effect is the same: a single list containing all the components of both sets and lists.
This is because the extend() method first transforms any iterable to list before adding all of the items of the iterable to the provided list to produce a single list. This applies to tuples, sets, and so on.
Both the extend() and add() methods in Python do the identical functionality. But, utilizing these techniques in Python is quite similar. The primary distinction between these two methods is that append() appends the whole list sent to it as a single element to the end of the given list, whereas extend() iterates through the argument and appends each element to the given list.