Python List insert() Method
“A list element is added to the list at the particular index using the Python method “list.insert()”. This writing is a guide to understanding the utilization of python’s “list.insert()” method with the help of practical example python programs.”
The syntax for utilizing the “list.insert()” method in python is provided below:
As the syntax shows, it takes two parameters as input. The “Index Number” takes in the specific index position where we want to add the element. And the “Element to insert” is the element that needs to be inserted into the existing list.
We will practice various techniques to employ this method.
Example # 1: Using a list.insert() Method to Insert a Single Element to a List
Elements can be included in a Python list using the “list.insert()” method. With the help of a simple illustration, we will see how we can insert an element into a list.
We have created an example program in the snapshot above. This code snippet begins by creating a Python list of elements. We have created a variable “alphabet” and assigned it a list of values as “p”, “q”, “r”, “t”, and “u”. To insert an extra element into this existing list, we have to invoke the Python “list.insert()” method. The list name, which is “alphabet” is supplied with the “.insert” method, and within the round braces of the “.insert” method, we have to specify the index number and then the element that has to be added to the list.
The index number we want the element to be added to is provided as 3, whereas the element that we need to insert into the list is “s”. This method will insert the element “s” at the 3rd index of the list, which is the fourth position. So “s” will be inserted after “r” into the list. To see the updated list on the output screen, python provides us with a very simple yet efficient method which is “print()”. This function will take in two parameters here; a text that will be displayed the same, i.e., “Updated_aplha:” and then we have provided the variable storing the list as “alphabet”.
The output projectile in the above image is obtained by compiling the python code snippet we explained earlier. Here we have a list being displayed after inserting the new element into it, which is “s” and is correctly appended at the 4th position of the list.
Example # 2: Using a list.insert() Method to Insert Multiple Elements to a List
The above instance explains the insertion of a single element into a python list; using the same technique, more than one element can be appended. To elaborate on the process, we have generated an example which is shown in the image below:
This excerpt illustrates that we have first initialized a variable with the following list of integer values: “111, 112, 113, 114, 115, 116, and 118”. We need to insert the number “500” in this list at the 2nd index or 3rd position. For the requirement, the “list.insert()” is called. The keyword “list” is replaced by the list name as “page” and as the arguments of this function, at the index place, “2” is inserted, and at the element place, “500” is supplied.
Ultimately, we have put the list into view using the “print()” method. This list will hold a new element in it. Now to add another element, the “list.insert()” is invoked again with a new index and element that has to be inserted. Then we will simply exhibit the list.
This resultant image shows two lists. The first one has only one new element, “500” whereas the 2nd list holds both the previous element, “500” and the new element, “700” in the specified index places.
This way, you can add as many elements to the list as required.
Example # 3: Using a list.insert() Method to Insert Tuple to a List
Apart from adding single or multiple elements, you can also add a tuple as an element to the python list. A tuple is python’s built-in ordered collection of elements that cannot be modified and are specified within round brackets. Let’s create an example code to learn this technique.
A variable “Names” is created and is initialized with the list of string values. The elements it holds are “Alex”, “Leo”, “Jennifer”, “Isabella”, and “Sophia”. Now we shall create a tuple that will be added to this list. A tuple with the values “Robert”, “James” and “George” is stored in the variable “Name_tuple”. To append this tuple into the Python list “Names”, we will call the “list.insert()” method. The list name is provided as “Names” with the “.insert()” function.
Within the parentheses of the function, the first place takes in the index number, which we specified as “4” and the 2nd place takes the element as input; here, we will provide the tuple “Name_tuple”. So, the tuple stored in the “Name_tuple” will be inserted at the 4th index or 5th position into the list “Names”. Finally, to see the output list on the terminal, we have employed python’s “print()” method with a string statement as “This is the list of Names:” and then the list name as “Names”.
A list with a tuple added as an element of the list at the 4th index can be viewed on the window when we hit the “Run code” command to execute the program.
Example # 4: Using a list.insert() Method to Insert Set to a List
A set can be added to a list at a specific location using the “list.insert()” function. We will be considering a practical example program provided below to understand the insertion of a set into the python list.
The first step, as we took out in all the previously constructed demonstrations, is to create a list of elements. We have a list “NumberList”, and it has elements as “10, 20, 30, 40, and 50”. Then in the next line of the code, a set of values is associated. The name of the set is “Num_set” and the values it is storing are “1”, “2”, “3”, and “4”. To insert this set into the python list we created earlier, the “list.insert()” function is invoked.
The index number where the set would be appended is specified as “1” which is the 2nd position in the list. And then, we provided the set to be added to the provided index. Then we passed the list “NumberList” to the “print()” function to display its content on the screen.
Here is the output generated by compiling the exceeding code snippet. We can observe that the set is added at the 1st index, the 2nd position in the list.
This article discusses the use of python’s “list.insert()” method for adding additional elements to the existing list whenever required. There are different techniques to add elements to a list. In this writing excerpt, we have discussed four techniques to implement this method. You can add a single element, multiple elements, a tuple, and a set as an element into a python list. Each method is elaborated on and explained precisely and in the easiest way to make them understandable.