Hi,
Using insert() method you can insert the elements in given list at required index.
Syntax:
list_name.insert(index, element)
Now coming to your code:
Input:
a = [2, 5, 7, 8, 9]
#element to be inserted
Element = 3
#element to be inserted before
beforeElement = 7
#find index
index = a.index(beforeElement)
#insert element at beforeElement
a.insert(index, Element)
print(a)
#Similarly to insert 4 after 3 in the list:
Element = 4
beforeElement=7
index= a.index(beforeElement)
a.insert(index, Element)
print(a)
Output:
[2, 5, 3, 4, 7, 8, 9]
As asked above, we have 3 and 4 after 5 in the list as output