list.insert python 3 code example

Example 1: python insert

# The insert() method inserts an element to the list 
# at a given index.
# Syntax: list_name.insert(index, element)
my_list = ["Add", "Answer"]
my_list.insert(1, "Grepper")
print (my_list)
> ['Add', 'Grepper', 'Answer']

Example 2: insert python3

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

#a.insert(index_to_insert_at, num_to_insert)
a.insert(0, -1)

# a is now: [-1,1,2,3,4,5]

Example 3: python insert list

# list.insert(before, value)
list = ["a", "b"]
list.insert(1, "c")
print(list)     # ['a', 'c', 'b']