How to remove items from a list in Python?

Python list data type can hold multiple different types of values in a single variable. It is a collection of ordered and changeable data items.

Python provides various methods to remove list items this includes remove(), pop(), and clear() apart from this you can also use the del keyword to delete items from a list.

In this article, you will learn different ways to remove items from a list in Python.

Examples of Python list

Some examples of Python lists are given below. A list in Python can hold different types of elements.

# The list x contains only numbers
x = [1, 2, 3 , 4, 5 ]

# The given list y contains only strings
y = ['a', 'b', 'c', 'd', 'e']

# The list z contains a mix of numbers and string
z= [1, 2, "abc",4, 'Hi']

# You can also use list inside a list
list_2d= [[3, 2, 1], [6, 5, 4], [9, 8, 7]]

Delete list items using Python remove() method

The remove() method is used to remove an item from a list in Python. You need to specify the item that you want to remove. If there are multiple occurrences of an item in a list then its first occurrence will be removed.

If a specified item is not found then ValueError will be raised. Now you can see the example which is given below.

my_list= [1, 2, "abc",4, 'Hi', 1, 'World']

# pass the name to remove an item

my_list.remove('abc')

# this will remove 1 that comes first in the list

my_list.remove(1)

# print the updated list

print(my_list)

You can see the output in the given image –

Remove list items using pop() method

The pop() method is also used to delete elements from a list. Using this method you can delete the list items by their index. It returns the value of the element at the passed index.

If you do not pass the index then it will remove the last item from the list.

You can see the usage of the pop() method in the given example.

my_list= [1, 2, "abc",4, 'Hi', 1, 'World']

# pass the index to remove an item

my_list.pop(2)

# this will remove the last item of the list

my_list.pop()

# print the updated list

print(my_list)

When you execute this program it will produce the given output.

Using the clear() method to remove list items

The clear() method will remove all the items from a list you cannot use to remove a specified item. It doesn’t take any argument and returns any value.

The following example shows the usage of the clear() method to remove list items.

my_list= [1, 2, "abc",4, 'Hi', 1, 'World']

# remove all items of the list

my_list.clear()

# print the updated list

print(my_list)

This will print the empty list as you can see in the given image.

Remove list items using del keyword

To remove an item from the list, you can use the del keyword followed by a list. You have to pass the index of the element to the list. The index will start with 0.

For example –

my_list= [1, 2, "abc",4, 'Hi', 1, 'World']

# remove item at index 3 of list

del my_list[3]

# print the updated list

print(my_list)

This will produce the given output.

You can also remove a range of list items by using the del keyword.

For example –

my_list= [1, 2, "abc",4, 'Hi', 1, 'World']

# remvoe items from index 1 to 4
del my_list[1:4]

# print the updated list

print(my_list)

This will delete the items from index 1 to 4 and print the updated list.

Conclusion

You have seen different ways to delete items from a list. Now if you have a query then write us in the comments below.

Leave a Comment