Peggy
0
Q:

list insert python

list.insert(index, element)
11
# The insert() method inserts an element to the list 
# at a given index.
# Syntax: list_name.insert(index, element)
my_list = ["Add", "Answer"]
my_list.insert(1, "Grepper")
print (my_list)
> ['Add', 'Grepper', 'Answer']
25
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
8
append(): append the object to the end of the list.
insert(): inserts the object before the given index.
extend(): extends the list by appending elements from the iterable.
4
# Python3 program for Insertion in a list   
# before any element using insert() method  
  
list1 = [ 1, 2, 3, 4, 5, 6 ] 
  
# Element to be inserted  
element = 13 
  
# Element to be inserted before 3 
beforeElement = 3 
  
# Find index 
index = list1.index(beforeElement)  
  
# Insert element at beforeElement  
list1.insert(index, element)  
print(list1) 
0
ls=[]
ls.append('Apple')
ls.append('Mango')
for i in ls:
	print(i)
  
-1
[1, 2, 13, 3, 4, 5, 6]
-1

New to Communities?

Join the community