“Dodatek vs wstaw Python” Kod odpowiedzi

Python List Insert vs.

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

Dodatek vs wstaw 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

Odpowiedzi podobne do “Dodatek vs wstaw Python”

Pytania podobne do “Dodatek vs wstaw Python”

Więcej pokrewnych odpowiedzi na “Dodatek vs wstaw Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu