Skip to content

How to remove an element from a list in Python (in 3 ways)

How to remove an element from a list in Python

In this tutorial we will learn how to remove an element from a list in Python . Python list has 3 built-in methods to remove an element from a list . They are as follows –

  • Python List pop() method
  • Python List remove() method
  • Python List clear() method

Now we will see about all these methods with examples –

Python List pop() method

Python list pop() method helps us to remove an element from a given list also returns the removed element .

Syntax of pop() method

list.pop([Index])

Parameters of pop() method

Index = Optional . It is an integer number specifying the index of the element which will be removed . Default value is (length of the list -1) . It means if we do not give any index it will remove the last element from the list .

Return type of pop() method

It returns the removed element .

Examples of Python List pop() method

Example 1: Remove the last element from a list in Python

# declaring a list
mylist = ["python","c","c++","java"]

# removing last element and return it
popitem=mylist.pop()

# printing the updated list
print(mylist)

# printing the deleted element
print("Removed element =",popitem)
['python', 'c', 'c++']
Removed element = java
  • In the above code first we have declared a list named mylist as ["python","c","c++","java"]
  • Then we have removed an element from the list and stored the removed element in popitem variable . As we have not given any argument in the pop() method so it will remove the last element .
  • Then we have printed the updated list . From the output we can see that last item "java" has been removed from the list .
  • After that we have printed the popitem variable which is printed "java" in the output .

Example 2: Remove the first element from a list in Python

# declaring a list
mylist = ["python","c","c++","java"]

# removing first element and return it
popitem=mylist.pop(0)

# printing the updated list
print(mylist)

# printing the deleted element
print("Removed element =",popitem)
['c', 'c++', 'java']
Removed element = python

In the above code we have written same code as the previous code but written pop(0) instead of pop() . For this reason first element has been removed from the list .

Python List remove() method

Python list remove() method helps us to remove the first occurrence of an element with specified value from a list . If the value is not found then it will raise ValueError .

Syntax of remove() method

list.remove(Value)

Parameters of remove() method

Value = Value of the element which we want to remove from the list .

Return type of remove() method

It returns None .

Examples of Python List remove() method

Example 1: Remove an element from a list in Python

# declaring a list
mylist = ["python","c","c++","java"]

# removing java
mylist.remove("java")

# printing the updated list
print(mylist)
['python', 'c', 'c++']
  • In the above code first we have declared a list named mylist as ["python","c","c++","java"]
  • Then we have removed "java" from the list using remove() method .
  • After that we have printed the list . From the output we can see the "java" has been removed from the list .

Example 2:

# declaring a list
mylist = ["python","c","c++","java"]

# removing javascript
mylist.remove("javascript")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_12156/4208589380.py in <module>
      3 
      4 # removing javascript
----> 5 mylist.remove("javascript")

ValueError: list.remove(x): x not in list

In the above code we have tried to remove "javascript" from the list . As "javascript" is not found in the list it has raised a ValueError .

Python List clear() method

Python list clear() method helps us to remove all items from a list .

Syntax of clear() method

list.clear()

Parameters of clear() method

It does not have any parameter .

Return type of clear() method

It returns None .

Examples of Python List clear() method

Example 1: Remove all element from a list in Python

# declaring a list
mylist = ["python","c","c++","java"]

# removing all element
mylist.clear()

# printing the updated list
print(mylist)
[]
  • In the above code first we have declared a list named mylist as ["python","c","c++","java"]
  • Then we have applied clear() method on the list .
  • After that we have printed the list . From the output we have seen that all the elements are removed from the list and an empty list is printed .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply