“tambahkan vs insert python” Kode Jawaban

Daftar Python Sisipkan Vs Append

Use of Append:

list = [1,2,3,4,5]

list.append(6)

print(list) # [1,2,3,4,5,6]

Use of Insert:

list = [1,2,3,4,5]

list.insert(5, 10) # [1,2,3,4,5,10]

list.insert(1, 10) # [1,10,3,4,5]
David Cao

tambahkan vs insert python

list_of_names = ["John", "Mike"]
print(list_of_names)
 
list_of_names.insert(0,"Amy")  #insert Amy as the first item, at index 0
print(list_of_names)
 
list_of_names.insert(2,"Mario") #insert Mario as the third item, at index 2
print(list_of_names)

#['John', 'Mike']
#['Amy', 'John', 'Mike']
#['Amy', 'John', 'Mario', 'Mike']

list_of_names = [] #create an empty list
print(list_of_names)
 
list_of_names.append("Greg")
print(list_of_names)
 
list_of_names.append("Mario")
print(list_of_names)
 
list_of_names.append("Maria")
print(list_of_names)

#[]
#['Greg']
#['Greg', 'Mario']
#['Greg', 'Mario', 'Maria']
David Cao

Jawaban yang mirip dengan “tambahkan vs insert python”

Pertanyaan yang mirip dengan “tambahkan vs insert python”

Lebih banyak jawaban terkait untuk “tambahkan vs insert python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya