Adding Items by Position to a List in Python
We can also add elements to a specific position in the list. To do this, use the insert method. In its first parameter, we pass the desired position number (index). In the second parameter, we specify the element to be added:
lst = ['a', 'b', 'c', 'd']
lst.insert(1, '2')
print(lst) # ['a', '2', 'b', 'c', 'd']
Given a list:
lst = [1, 2, 3, 4, 5]
Add an element with a value after the two 'a'.
Given a list:
lst = [1, 2, 3, 4, 5]
Add an element with a value to the beginning of the list 'a'.
Given a list:
lst = [3, 6, 12]
Add 9 before the number 12 in the list.