Question
How can I add an item to the beginning of a list?
Answer
We learned about a function called insert()
that accepts 2 arguments: the position to insert at and the value to insert. If we had a list called my_list
that contained the list [1, 2, 3, 4]
and we used my_list.insert(0, 5)
, the resulting list would be [5, 1, 2, 3, 4]
. This is because the 0th index is the beginning of the list!
5 Likes
Whatâs the different between inserting in to and appending a list?
With lists you can do certain things with great efficiency: access an item by its index, change the value of an item at a certain index, and add or remove an item at the end of the list.
Even as a beginner, imagine a list as a series of consecutive memory locations. You can suppose that, knowing the first address and calling it index zero, it would be fairly easy to go straight to any index and read or change the value that is stored there, and to add a value to the end of the list.
But what if you wanted to insert an item into the middle of the list? Youâd need to write code to take every item to the ârightâ of the insertion point, move it over, and give it new address. Itâs the same with remove(), or pop() with any index but the last.
Thatâs why we say that lst.insert() requires more overhead than lst.append().
2 Likes
So you would insert to every index in a list apart from after the last used index, where you .append a new index?
âŚwhy didnât they just make it all inserting? There must be a reason for this later? Or is it just a historic thing that will get amended in a new version of Python?
Yes, and using insert would consume much more time than using append. (Remember that lists can have millions of elements.)
It seems to me to be perfectly logical to have a few methods - append(), default pop(), and identifying an element by its index: lst[idx] - that leverage the strengths of the list structure, while also having others, such as insert(), remove() and pop() from anywhere but the end that perform tasks you might need done, but at a greater cost.
Appending is like tossing a piece of paper on top of a pile. Inserting is like lifting up certain papers to set the paper between two other papers. If you insert, you need to know where the paper is going in the stack.
Imagine you have a stack of 100 papers, with the bottom paper being in position â0.â You have an extra paper, and you add it to the top. Then you tell your personal assistant to go get paper 52 out of the stack. If you had instead âinsertedâ the new paper somewhere in the middle of the stack, your assistant might go get the paper in position 52 and it isnât the paper you wanted anymore, because there was just an extra paper inserted at, say, position 20. Now the paper you thought was at position 52 is now at position 53. Chaos!
If you wanted to âinsertâ the paper at the top of the stack (to avoid disturbing the order of the existing papers), you could either use append, and python places it on the top of the pile, OR you could use insert, but then you would also need to know how many papers are in your stack, because insert must have two pieces of information â what to insert and where to insert it.
n = [1, 3, 5]
# Append the number 4 here
print(n)
it will be something like this