Difference between insert and append?


What is the difference?

Exercise 3/18 - I know how to add to the list using append. Could I also add to the list with
n.insert(3,4) code?
It returned this code as an error.


You say there was an error, I’d guess that if you read it, it’ll tell you why you could not do that, or whatever it was that couldn’t be done

Also, it’s faster to add at the end because otherwise elements have to be moved to make room, if you’re inserting a lot to the same list then you’re probably not using an appropriate data structure, or perhaps you should reordering afterwards instead of doing lots of unnecessary re-ordering while adding elements

You can read about append and insert in the documentation, that’s something you generally should do for almost anything that you use, how else would you know that it does what you want?
You can also use the help function to print out their docstrings

>>> help([].insert)
Help on built-in function insert:

insert(...) method of builtins.list instance
    L.insert(index, object) -- insert object before index


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.