“append vs python einfügen” Code-Antworten

Python -Liste Einfügen gegen Anhänge

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

append vs python einfügen

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

Ähnliche Antworten wie “append vs python einfügen”

Fragen ähnlich wie “append vs python einfügen”

Weitere verwandte Antworten zu “append vs python einfügen” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen