You'll have to pass the new ordinal position to insert using len in this case:
In [62]:
a=[1,2,3,4]
a.insert(len(a),5)
a
Out[62]:
[1, 2, 3, 4, 5]
Answer from EdChum on Stack OverflowYou'll have to pass the new ordinal position to insert using len in this case:
In [62]:
a=[1,2,3,4]
a.insert(len(a),5)
a
Out[62]:
[1, 2, 3, 4, 5]
list.insert with any index >= len(of_the_list) places the value at the end of list. It behaves like append
Python 3.7.4
>>>lst=[10,20,30]
>>>lst.insert(len(lst), 101)
>>>lst
[10, 20, 30, 101]
>>>lst.insert(len(lst)+50, 202)
>>>lst
[10, 20, 30, 101, 202]
Time complexity, append O(1), insert O(n)
Videos
some_list[-1] is the shortest and most Pythonic.
In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)], which gives you the first element.
You can also set list elements in this way. For instance:
>>> some_list = [1, 2, 3]
>>> some_list[-1] = 5 # Set the last element
>>> some_list[-2] = 3 # Set the second to last element
>>> some_list
[1, 3, 5]
Note that getting a list item by index will raise an IndexError if the expected item doesn't exist. This means that some_list[-1] will raise an exception if some_list is empty, because an empty list can't have a last element.
If your str() or list() objects might end up being empty as so: astr = '' or alist = [], then you might want to use alist[-1:] instead of alist[-1] for object "sameness".
The significance of this is:
alist = []
alist[-1] # will generate an IndexError exception whereas
alist[-1:] # will return an empty list
astr = ''
astr[-1] # will generate an IndexError exception whereas
astr[-1:] # will return an empty str
Where the distinction being made is that returning an empty list object or empty str object is more "last element"-like then an exception object.
When you use insert, you are inserting at the specified index position. Remember that lst[-x] is the same as lst[len(lst)-1-x]. For example, lst[-1] and lst[len(lst)-1-1] both refer to the last index position in the list. Once you know this, the behavior you are seeing with the negative index positions starts to make more sense:
nums = [1, 2, 3, 4, 5]
nums.insert(1, 'Hello') # Insert at index position 1.
# Result: [1, 'Hello', 2, 3, 4, 5]
nums.insert(-1, 'Bye') # Insert at index position len(nums)-1-1 = 4.
# Same as: nums.insert(4, 'Bye')
# Result: [1, 'Hello', 2, 3, 4, 'Bye', 5]
nums.insert(-2, 'Good') # Insert at index position len(nums)-1-2 = 5.
# Same as: nums.insert(5, 'Good')
# Result: [1, 'Hello', 2, 3, 4, 'Good', 'Bye', 5]
To insert at the end of the list using insert:
nums = [1, 2, 3]
nums.insert(len(nums), 'Hello')
# Result: [1, 2, 3, 'Hello']
As you can see, 'Hello' was inserted in index position 3 (the original len(nums)) as specified.
If all you want to do is add an element at the end of a list, you could use append instead:
nums = [1, 2, 3]
nums.append('Hello')
# Result: [1, 2, 3, 'Hello']
To insert at the end of a list, you have to use
numbers.insert(len(numbers), 'Hello')
This will output
[5, 4, 3, 2, 1, 'Hello']
The problem you faced occurs because numbers[-i] is equivalent to numbers[len(numbers) - 1 - i]. So, numbers[-1] is actually numbers[5 - 1 - 1], i.e., numbers[3], which is exactly what was happening in your program. Insertion was happening at the 3rd index.
You can get last element using list[-1] which is cool. Python is cool.
And if you really want list.append() to return you a value you should create your own class inherited from list and redefine append() function
From the Python documentation:
"You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed โ they return the default None. 1 This is a design principle for all mutable data structures in Python."