EN
Python - add list items
0 points
In this article, we would like to show you how to add items to the lists in Python.
In this example, we use append()
method to add an item at the end of the list.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
my_list.append('D')
3
4
print(my_list) # ['A', 'B', 'C', 'D']
Output:
xxxxxxxxxx
1
['A', 'B', 'C', 'D']
In this example, we use insert()
method to insert an item at the specified index.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
my_list.insert(1, 'X')
3
4
print(my_list) # ['A', 'X', 'B', 'C']
Output:
xxxxxxxxxx
1
['A', 'X', 'B', 'C']