insert() function in python by R4R Team

- insert() is a python in-build function which is applicable on the list which insert the new item in the list at per the index value.

Syntax-

list.insert(index,item)

- insert() take two argument ,one is index value and another one is item which are to be inserted

Example

list is [1,2,3,4]
add 5 at index 2 will give [1,2,5,3,4]


program-

l=[1,2,3]
print("List is ",l)

l.insert(1,"Insert1")
print("list is ",l)

l.insert(0,"Insert0")
print("list is ",l)

l.insert(3,"Insert3")
print("list is ",l)

l.insert(2,"Insert2")
print("list is ",l)


output-

List is [1, 2, 3]
list is [1, 'Insert1', 2, 3]
list is ['Insert0', 1, 'Insert1', 2, 3]
list is ['Insert0', 1, 'Insert1', 'Insert3', 2, 3]
list is ['Insert0', 1, 'Insert2', 'Insert1', 'Insert3', 2, 3]


-In this program, we take a list initially it is [1,2,3] and then we insert different items at different index and print the list one by one.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!