“Ajouter vs Insérer Python” Réponses codées

INSERT LISTE PYTHON VS APPENDRE

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

Ajouter vs Insérer 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

Réponses similaires à “Ajouter vs Insérer Python”

Questions similaires à “Ajouter vs Insérer Python”

Plus de réponses similaires à “Ajouter vs Insérer Python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code