This article will learn how to add and remove elements from a list in Python.
Take L as an example:
l=[‘hello‘,‘tomorrow‘,‘!‘]
1. Increase:
(1) Add elements at the end of the list: list name. Append (' element ')
l.append(‘hello‘)print(l)
Output:
(2) Insert an element anywhere in the list: list name. Insert (index, ' element ')
L.insert (1, "Luu S")
Print (L)
Output:
2. Delete
(1) del list name [serial number]
del l[1]print(l)
(2) List name. Pop (ordinal)-------can be deleted and then used
print(l)pop_element=l.pop(0)print(l)print(pop_element)
Output:
(3) List name. Remove (' element ')-------can delete matching first element based on content
Print (L)
L.remove (' Hello ')
Print (L)
Output Result:
3. Find
To learn
4. Modifications
(1) Directly with the index, assignment to modify
l[1]=‘future‘print(l)
Output:
List additions and deletions in Python