One of the most common tasks in Python programming is to insert elements into a list. A list is a collection of items that are ordered and changeable. In this tutorial, we will discuss how to use the Python insert into list function.
What is Python Insert into List?
Python insert into list function is used to insert an element at a specified position in a list. The syntax of the insert()
function is as follows:
list.insert(index, element)
Here, list
is the name of the list, index
is the position where the element needs to be inserted, and element
is the value that needs to be inserted.
How to Use Python Insert into List?
Now that we know what the Python insert into list function is, let’s look at some examples to understand how it works.
Example 1: Inserting an Element at the Beginning of a List
fruits = ['apple', 'banana', 'cherry']
fruits.insert(0, 'orange')
print(fruits)
Output:
['orange', 'apple', 'banana', 'cherry']
In the above example, we have inserted the element ‘orange’ at the beginning of the list fruits
.
Example 2: Inserting an Element at a Specific Position
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, 'orange')
print(fruits)
Output:
['apple', 'orange', 'banana', 'cherry']
In this example, we have inserted the element ‘orange’ at the second position of the list fruits
.
Example 3: Inserting Multiple Elements at a Specific Position
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, 'orange', 'peach')
print(fruits)
Output:
TypeError: insert() takes exactly 2 arguments (3 given)
We get a TypeError because the insert()
function takes only two arguments, the index and the element to be inserted. To insert multiple elements, we can use slicing:
fruits = ['apple', 'banana', 'cherry']
index = 1
fruits[index:index] = ['orange', 'peach']
print(fruits)
Output:
['apple', 'orange', 'peach', 'banana', 'cherry']
In this example, we have inserted the elements ‘orange’ and ‘peach’ at the second position of the list fruits
.
Example 4: Inserting an Element at the End of a List
fruits = ['apple', 'banana', 'cherry']
fruits.insert(len(fruits), 'orange')
print(fruits)
Output:
['apple', 'banana', 'cherry', 'orange']
In this example, we have inserted the element ‘orange’ at the end of the list fruits
.
Example 5: Inserting an Element in a Sorted List
numbers = [1, 3, 5, 7, 9]
for i in range(len(numbers)):
if numbers[i] > 4:
numbers.insert(i, 4)
break
print(numbers)
Output:
[1, 3, 4, 5, 7, 9]
In this example, we have inserted the element 4 in the sorted list numbers
. We have used a loop to find the position where the element needs to be inserted.
Example 6: Inserting an Element at the nth Position from the End of a List
fruits = ['apple', 'banana', 'cherry']
fruits.insert(-1, 'orange')
print(fruits)
Output:
['apple', 'banana', 'orange', 'cherry']
In this example, we have inserted the element ‘orange’ in the second position from the end of the list fruits
.
Conclusion
Python insert into list function is a useful function that helps to insert elements at a specific position in a list. In this tutorial, we have discussed the syntax of the insert()
function and provided several examples that illustrate its usage. We hope that this tutorial has helped you understand how to use the Python insert into list function.