In python, the List
insert()
method inserts the specified value at a given index in a list.
list.insert(index, elmnt)
The following are the required parameters:
index
: specifies the position to insert the element.
elmnt
: is the element to be inserted.
The following code shows how to use the insert()
method:
even_no = [ 2, 4, 6, 8, 10 ]# insert 0 at the front of the listeven_no.insert(0, 0)print(even_no)letters = ['a', 'b', 'c', 'd', 'e']# insert f at 3th indexletters.insert(3, 'f')print(letters)
numbers = [ 7, 23, 1, 5, 16 ]# tuple of numbersnum_tuple = (6, 8, 9)# inserting a tuple to the listnumbers.insert(2, num_tuple)print(numbers)