More Examples on Python List insert() Method
Here is another examples to depict Python list insert at index 0:
Python3
list = [ 'Sun' , 'rises' , 'in' , 'the' , 'east' ] list .insert( 0 , "The" ) print ( list ) |
['The', 'Sun', 'rises', 'in', 'the', 'east']
Let’s see some of the scenarios with the python list insert() function to clearly understand the workings of the insert() function.
1. Inserting an Element to a specific index into the List
Here, we are inserting 10 at the 5th position (4th index) in a Python list.
Python3
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] # insert 10 at 4th index list1.insert( 4 , 10 ) print (list1) |
[1, 2, 3, 4, 10, 5, 6, 7]
2. Error of insert() Method
Here, we are inserting 1 at the 10th position in a Python list, we will get an error if we try to insert anything in a string because the string doesn’t have attribute insert().
Python3
# attribute error string = "1234567" string.insert( 10 , 1 ) print (string) |
Output:
Traceback (most recent call last): File "/home/2fe54bd8723cd0ae89a17325da8b2eb5.py", line 7, in string.insert(10, 1) AttributeError: 'str' object has no attribute 'insert'
3. Insertion in a List Before any Element
In the parse of Python List Insert here, we are inserting 13 at the 3rd position before 3 in a Python list.
Python3
# Python3 program for Insertion in a list # before any element using insert() method list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ] # Element to be inserted element = 13 # Element to be inserted before 3 beforeElement = 3 # Find index index = list1.index(beforeElement) # Insert element at beforeElement list1.insert(index, element) print (list1) |
[1, 2, 13, 3, 4, 5, 6]
4. Inserting a Tuple into the List
Here we are inserting a tuple in a list using the insert() function in Python.
Python3
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ] # tuple of numbers num_tuple = ( 4 , 5 , 6 ) # inserting a tuple to the list list1.insert( 2 , num_tuple) print (list1) |
[1, 2, (4, 5, 6), 3, 4, 5, 6]
5. Insert an Element to the Beginning of a List
In this example, we are inserting the “orange” string at the 0 index of the fruits list.
Python3
fruits = [ 'apple' , 'banana' , 'cherry' ] fruits.insert( 0 , 'orange' ) print (fruits) # Output: ['orange', 'apple', 'banana', 'cherry'] |
['orange', 'apple', 'banana', 'cherry']
6. Inserting an Element at the end of the List
In this example, we are inserting the “cherry” at the end of the list.
Python3
fruits = [ 'apple' , 'banana' , 'cherry' ] fruits.insert( - 1 , 'orange' ) print (fruits) # Output: ['apple', 'banana', 'orange', 'cherry'] |
['apple', 'banana', 'orange', 'cherry']
7. Inserting a dictionary to a list in Python
Here we are inserting a dictionary in a list using the insert() function in Python.
Python3
my_list = [{ 'name' : 'Alice' , 'age' : 30 }, { 'name' : 'Bob' , 'age' : 25 }] new_dict = { 'name' : 'Charlie' , 'age' : 40 } my_list.append(new_dict) print (my_list) |
[{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}, {'name': 'Charlie', 'age': 40}]
8. Python Insert List in Another List
Here we are inserting a list in a list using the insert() function in Python.
Python3
list1 = [ 1 , 2 , 3 ] list2 = [ 4 , 5 , 6 ] list1 = list1 + list2 print (list1) |
[1, 2, 3, 4, 5, 6]
9. Insert elements of a set to a list in Python
Here we are inserting a set in a list using the insert() function in Python.
Python3
list1 = [ 1 , 2 , 3 ] s = { 4 , 5 , 6 } list1.insert( 3 ,s) print (list1) |
[1, 2, 3, {4, 5, 6}]
Go to the below articles to get more details information about Python Insert() Function
In the above article, we have discussed the Python list insert() method and its parameters with suitable examples. Python insert() function is very useful when dealing with big data.
We hope this article taught you about how to use insert() in Python.
Python List insert() Method With Examples
Python List insert() method inserts an item at a specific index in a list.
Example:
Python3
# creating a list fruit = [ "banana" , "cherry" , "grape" ] fruit.insert( 1 , "apple" ) print (fruit) |
['banana', 'apple', 'cherry', 'grape']