Python 3 – List insert() Method
In Python, lists are an essential datatype that allows you to store a collection of items in a single variable. Lists come with several built-in methods to manipulate their contents. One such method is the insert()
method that allows you to insert an item at a specified position within a list. In this article, we’ll take an in-depth look at the insert()
method, including its syntax, parameters, and examples.
Syntax
The syntax for using the insert()
method is as follows:
list_name.insert(index, element)
Here, list_name
is the name of the list you want to insert an element into. index
is the index where you want to insert the element, and element
is the item that you want to insert.
Parameters
The insert()
method takes two parameters:
index
: The index where you want to insert the element.element
: The item that you want to insert.
Return Value
The insert()
method does not return any value. Instead, it modifies the list in-place by inserting the new element at the specified position.
Examples
Let’s look at some examples of how you can use the insert()
method to add items into a list.
Example 1: Insert an element at the beginning of the list
fruits = ["apple", "banana", "cherry"]
fruits.insert(0, "orange")
print(fruits)
Output:
['orange', 'apple', 'banana', 'cherry']
In this example, we inserted the element "orange"
at index 0
. As a result, the list is modified to ['orange', 'apple', 'banana', 'cherry']
.
Example 2: Insert an element at the end of the list
fruits = ["apple", "banana", "cherry"]
fruits.insert(len(fruits), "orange")
print(fruits)
Output:
['apple', 'banana', 'cherry', 'orange']
In this example, we inserted the element "orange"
at the end of the list. The len(fruits)
function returns the length of the fruits list, which is 3
. Therefore, "orange"
is inserted at the index 3
, and the resulting list is ['apple', 'banana', 'cherry', 'orange']
.
Example 3: Insert an element at a specific index
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print(fruits)
Output:
['apple', 'orange', 'banana', 'cherry']
In this example, we inserted the element "orange"
at index 1
. The resulting list is ['apple', 'orange', 'banana', 'cherry']
.
Example 4: Insert multiple elements into a list
fruits = ["apple", "banana", "cherry"]
fruits_to_insert = ["orange", "kiwi"]
for i in fruits_to_insert:
fruits.insert(len(fruits), i)
print(fruits)
Output:
['apple', 'banana', 'cherry', 'orange', 'kiwi']
In this example, we used a loop to insert all the elements in the fruits_to_insert
list into the fruits
list. As a result, the final list is ['apple', 'banana', 'cherry', 'orange', 'kiwi']
.
Conclusion
In this article, we covered the insert()
method of Python lists. We saw how to use the method to insert an element at the beginning, end, or a specific index of a list. We also looked at how to insert multiple elements into a list using a loop. The insert()
method is a simple but useful feature that allows you to modify lists efficiently.