We add a value to a specific index with insert()
. By coding insert(0, "lemon")
, we'll add the value to the start of the list.
shopping = ["kiwis", "peas"]
shopping.insert(0, "lemon")
print(shopping)
The insert()
function has two parameters. First, the index where we want to insert the value, followed by the value.
insert(1, "chocolate")
adds a value in the second position.
shopping = ["kiwis", "peas"]
shopping.insert(0, "lemon")
shopping.insert(1, "chocolate")
print(shopping)