Remove element from list in Python(clear, pop, remove, del)

Remove element from list in Python

Hello Techies,

In this tutorial, we will learn about How to remove element from list in Python with the help of examples.

How to remove element from list in Python with different List Methods & Keyword

To remove element from list in Python you can use List Methods that are clear(), pop(), and remove(). You can also use a del keyword to remove items from the list.

Let’s try to understand the list methods that help to remove elements from the list in Python. Below are the methods:

  1. clear()
  2. pop()
  3. remove()

1. clear()

The clear() method is used to delete all elements in the Python list.

Syntax of the clear() method:

list.clear()

Example of clear() method:

# Python program to clear a list using clear() method  

# Creating list 
data = [6, 0, 4, 1]
print('data list before clear:', data)

# Clearing list  
data.clear()
print('data list after clear:', data) 

Output:

data list before clear: [6, 0, 4, 1]
data list after clear: []

2. pop()

The pop() method is used to remove elements from a list based on a given index. The index value is optional. Suppose if you do not provide any index value it will remove the last element from the list.

Syntax of the pop() method:

list.pop(index)
pop() parameters:
  • The argument given to the method is optional. If not passed, the default index is given as -1 argument (index of the last item).
  • The pop () method takes a single parameter(index).
  • If the index passed in the method is not in the given range, it throws IndexError.

The index in the list starts from 0. For example, if you want to remove C from [‘A’, ‘B’, ‘C’, ‘D’, ‘E’] from your list, you have to provide 2 as an index. Let’s look at the same example:

# Python program to remove elements from a list using the pop() method

# Creating list 
data = ['A', 'B', 'C', 'D', 'E']
print('data list before pop:', data)

# Removing an element from the list
data.pop(2)
print('data list after pop:', data)

Output:

data list before pop: ['A', 'B', 'C', 'D', 'E']
data list after pop: ['A', 'B', 'D', 'E']

You can also use negative indexing to specify the position from the end. -1 index refers to the last element, -2 refers to the second last element, and so on.

Example: How do you pop a specific value from a list in Python?

# Python program to Remove element from list in Python using the 
# pop() method for negative indexing

# Creating list
data = ['A', 'B', 'C', 'D', 'E']
print('data list before pop:', data)

# Removing an element from the list
data.pop(-2)
print('data list after pop:', data)

Output:

data list before pop: ['A', 'B', 'C', 'D', 'E']
data list after pop: ['A', 'B', 'C', 'E']

Example: How do you remove the last element of a list in Python?

# Python program to Remove element from list in Python using the pop() 
# without passing index.

# Creating list
data = ['A', 'B', 'C', 'D', 'E']
print('data list before pop:', data)

# Removing an element from the list
data.pop()
print('data list after pop:', data)

Output:

data list before pop: ['A', 'B', 'C', 'D', 'E']
data list after pop: ['A', 'B', 'C', 'D']

How to solve pop index out of range error in python?

Suppose there are 10 elements in your list and if you try to pop 12 index it will throw an IndexError. To avoid such types of errors try to avoid the pop method or you can use Try-except Python Block.

# Python program to Remove element from list in Python using the pop() 
# for IndexError

# Creating list
data = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
print('data list before pop:', data)

# Removing an element from the list
try:
    data.pop(12)
    print('data list after pop:', data)
except Exception as e:
    print('Sorry unable to pop this item -', e)

Output:

data list before pop: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
Sorry unable to pop this item - pop index out of range

3. remove()

The remove() method is used to remove the first matching element (which is given as a parameter) from the list.

Syntax of the remove() method:

list.remove(element)

Example: Remove element from the list

# Python program to Remove element from list in Python using
# the remove() method

# Creating list
data = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
print('data list before remove:', data)

# Removing an element from the list
data.remove('D')
print('data list after remove:', data)

Output:

data list before remove: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
data list after remove: ['A', 'B', 'C', 'E', 'F', 'G', 'H', 'I', 'J']

Suppose the list contains duplicate elements, then the remove() method will delete the first matching element. Check out the example below:

# Python program to Remove element from list in Python using 
# the remove() method for duplicate elements

# Creating list
data = ['A', 'B', 'B', 'C', 'E']
print('data list before remove:', data)

# Removing an element from the list
data.remove('B')
print('data list after remove:', data)

Output:

data list before remove: ['A', 'B', 'B', 'C', 'E']
data list after remove: ['A', 'B', 'C', 'E']

del keyword to Remove element from list in Python

You can use del keywords followed by a list to remove an element from the list. You need to pass the index of the elements in the list. The index starts at 0.

Syntax of del keyword

del list[index]

Example 1: How do you use the del keyword in Python?

# Python program to Remove element from list in Python 
# using the del() method

# Creating list
data = ['A', 'B', 'B', 'C', 'E']
print('data list before del:', data)

# Removing an element from the list
del data[4]
print('data list after del:', data)

Output:

data list before remove: ['A', 'B', 'B', 'C', 'E']
data list after remove: ['A', 'B', 'B', 'C']

Example 2: How do I remove multiple elements from a list in Python?

# Python program to remove element from a list in python
# using the del() method using slice

# Creating list
data = ['A', 'B', 'B', 'C', 'E']
print('data list before del:', data)

# Removing an element from the list
del data[2:4]
print('data list after del:', data)

Output:

data list before del: ['A', 'B', 'B', 'C', 'E']
data list after del: ['A', 'B', 'E']

Conclusion

In this tutorial, you learned about Remove element from list in Python(clear, pop, remove, del), We have covered the following points:

  • Python clear() method
  • Python pop() method
  • Python remove() method
  • Python del keyword

Leave a Comment