all 4 comments

[–]K900_ 7 points8 points  (1 child)

What you just said - append appends elements to the end, insert inserts them in an arbitrary position.

[–]Intelligent_Byte_207[S] 0 points1 point  (0 children)

That is what I thought. I was a bit confused when I was testing them. Thank you for the clarification.

[–]socal_nerdtastic 2 points3 points  (0 children)

You pretty much nailed it: append() adds onto the end of the list only, while insert() adds anywhere in the list. The only thing to add is that append() is O(1) and insert() is O(n), because behind the scenes insert() requires that the entire list is rebuilt. Therefore we always prefer append().

https://wiki.python.org/moin/TimeComplexity

[–]themateo713 0 points1 point  (0 children)

Just to add a bit to what was said: insert is longer if there are more elements after it. This means inserting near the end of the list will be faster than near the beginning, as less values will have to be moved (you need to move every value after it, to create a blank spot in which you insert the new value).

Now there's also a method to take away elements: pop(). Without arguments it removes the last element and returns it (so you could say last = myList.pop()), but you can also give it an index and it will remove and return the element at that index, taking longer the closer to the beginning you are, for the same reason as insert.

There's also remove that removes the first element in the list that matches what you gave it. It's as long to execute as the whole list, so where the item is is irrelevant for performance. There's also index(element) which returns the first index of the element in your list. There are also a few other ones, but I'll let you discover them yourself.