how to add an element at the end of the list in python code example
Example 1: how to insert an element to the end of the list using insert in python
Syntax : list(index, item)
>>> numbers = [1, 2, 3]
>>> numbers
[1, 2, 3]
>>> numbers.insert(len(numbers), 4)
>>> numbers
[1, 2, 3, 4]
>>> numbers.insert(4, 5)
>>> numbers
[1, 2, 3, 4, 5]
>>> len(numbers)
5
>>> numbers[4]
5
>>> numbers[5]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
numbers[5]
IndexError: list index out of range
Example 2: how to add to the end of an array python
array = [1 , 2, 3]
print(array)
array.append(4)
print(array)