+2 votes
in Programming Languages by (11.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

0 votes
by (21.2k points)
edited by

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]
>>>

...