Remove Element From List In Python

Python list type is used to store multiple values or elements in a single variable or object. In some cases, we may need to remove all, single or multiple items from the list. There are different ways and methods to remove items in a Python list.

Remove All Elements with clear() Method

Python list type provides the clear() method which is simply used to clear or remove all items in a list. This clear() method makes a list empty.

names = ['ismail','ahmet','ali','elif']


Remove Element By Index with pop() Method

The list type provides the pop() method which is used to remove the specified element by its index number. The pop() method accepts the element index number as a parameter. The popped element is returned as a return value.

names = ['ismail','ahmet','ali','elif']

element = names.pop(0)



Remove Element By Value with remove() Method

The remove() method is used to remove an element by specifying the element as a parameter to the remove() method. In the following example, we remove the element “ismail” from the list “names”.

names = ['ismail','ahmet','ali','elif']

element = names.remove("ismail")

Remove Element by Index or Slice with del Statement

The del statement is used to remove a specified element from the list. The list is provided with the element number to the del statement like below. In the following example, we remove the first element from the list named “names”.

names = ['ismail','ahmet','ali','elif']

del names[0]

Remove Element with List Comprehension

List comprehension is a method to iterate over a list and apply some conditions to select some values during this iteration. We can use the list comprehension in order to remove a specific element. In the following example, we remove the element “ismail”.

names = ['ismail','ahmet','ali','elif']

new_names = [s for s in names if s != 'ismail']


Leave a Comment