Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

append 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']
Comment

python list insert 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]
Comment

PREVIOUS NEXT
Code Example
Python :: python os 
Python :: pd.loc 
Python :: python try 
Python :: null=true django 
Python :: django collectstatic with auto yes 
Python :: Python Global in Nested Functions 
Python :: python slice last 2 items of list 
Python :: comment transformer un chaine de caractere en liste python 
Python :: godot get scenes from folder 
Python :: smote on dataframe of feature 
Python :: how to get max value and min values in entire dataframe 
Python :: how to append to an empty dataframe pandas 
Python :: recorrer lista desde el final python 
Python :: pandas combine year month day column to date 
Python :: Returns a DataFrame representing the result of the given query 
Python :: alphabetical 
Python :: list object attributes python 
Python :: List get both index and value. 
Python :: python requests cannot find existing url 
Python :: even in python 
Python :: python triangular number 
Python :: python website example 
Python :: gui python 
Python :: choice without replacement python 
Python :: twitter scraping python 
Python :: how to set a hyperlink in python 
Python :: how to remove whitespace from string in python 
Python :: get output of a function in a variable python 
Python :: convert pdf to word doc in python 
Python :: pass query params django template 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =