Oct-01-2020, 03:05 AM
Your confusion makes sense. It took me a moment to realize it too. You're thinking that it inserts intp the index you provide. This is not quite correct.
https://docs.python.org/3.8/tutorial/dat...tures.html (emphasis added) Wrote:Insert an item at a given position. The first argument is the index of the element before which to insert, soGiven this, the behavior makes sense: -1 is the index of the last element, so you insert before it.a.insert(0, x)
inserts at the front of the list, anda.insert(len(a), x)
is equivalent toa.append(x)
.