+2 votes
in Programming Languages by (73.8k points)

I want to insert an element at some particular position in a list. How can I do it?

E.g.

Original list:

a=[11,23,345,56,23]

New list:

a=[11,23,45,345,56,23]

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

There is a method "insert()" for the list data type. You can use the method to insert an item at a particular location.

list.insert(i, x), where i=position and x=new item

E.g.

>>> a=[11,23,345,56,23]
>>> a.insert(2,45)
>>> a
[11, 23, 45, 345, 56, 23]
>>>


...