Use the remove() function to remove a specific item from the list.
colors_list = ["Red", "Blue", "Green"]
colors_list.remove("Blue")
print(colors_list)
['Red', 'Green']
In the above example, we removed the color "Blue" from a list using the remove() function. You can see the output "Blue" color removed from the list, and the remaining color items are displayed.
Use the pop() function to remove a specific index from the list.
colors_list = ["Red", "Blue", "Green"]
colors_list.pop(1)
print(colors_list)
['Red', 'Green']
In the above example, we removed the index item position 1 from a list using the pop() function. You can see the output "Blue" color removed from the list because the index position of the "Blue" color is 1, and the remaining items are displayed.
When you do not specify the index position in pop(), it will remove the last item from the list.
colors_list = ["Red", "Blue", "Green"]
colors_list.pop()
print(colors_list)
['Red', 'Blue']
In the above example, we used the pop() function without specifying the index position, so the last index position item "Green" color removed from the list, and the remaining items are displayed.
Use the del keyword to remove a specific index from the list.
colors_list = ["Red", "Blue", "Green"]
del colors_list[0]
print(colors_list)
['Blue', 'Green']
In the above example, we removed the index item position 0 from a list using the del keyword. You can see the output "Red" color removed from the list because the index position of the "Red" color is 0, and the remaining items are displayed.
Use the del keyword to delete the entire list.
colors_list = ["Red", "Blue", "Green"]
del colors_list
print(colors_list)
NameError: name 'colors_list' is not defined
We removed the entire list using the del keyword. You can see the output "NameError: name 'colors_list' is not defined".
Use the clear() function to empty the list. It will delete all items from the list, but the list will still exist.
colors_list = ["Red", "Blue", "Green"]
colors_list.clear()
print(colors_list)
[]
Using the clear() function, we removed all items from the list. In the output, you can see that the square brackets ([]) are empty.