Python Remove item from List [remove(), pop(), clear(), del]

Python remove item from list | In Python, there are many methods available on the list data type that help you remove an element from a given list. In this post, we will discuss how to remove items or elements from the list using remove(), pop(), clear(), and del operator. We will take a list while declaring the variables then the Python program removes the element from the list. Finally, the new list will be displayed on the screen.

Python Remove Element From List

Python list method remove() searches for the given element in the list and removes the first matching element. The method does not return any value but removes the given object from the list. Syntax: list.remove(element)

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed HTML from the list
my_list.remove('HTML')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Python’, ‘Javascript’]

If the given element does not exist in the list, remove() method are getting ValueError: list.remove(x): x not in list exception.

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'Javascript']

# printing original list
print('List:', my_list)

# removed HTML from the list
my_list.remove('HTML')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘Javascript’]
Traceback (most recent call last):
File “main.py”, line 10, in
my_list.remove(‘HTML’)
ValueError: list.remove(x): x not in list

Python Remove Duplicates from List

In the previous program, the list contains a unique element but in this program, we are getting a list that contains duplicate elements, then the remove() method only removes the first matching element.

# Python program to duplicates item from list

# take list
my_list = ['C', 'Java', 'Python', 'Java', 'Javascript', 'Java']

# printing original list
print('List:', my_list)

# removed Java from the list
my_list.remove('Java')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘Java’, ‘Javascript’, ‘Java’]
New list: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Java’]

Python Remove Element from List by index

Python provides built-in function pop() that removes and returns last value from the list or the given index value. Syntax: list_name.pop(index)

# Python program to remove element from list by index

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed index 3 item from the list
my_list.pop(3)

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Python’, ‘Javascript’]

Note: Index in Python starts from 0, not 1.

The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last item).

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed item from the list
my_list.pop()

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Python’, ‘HTML’]

If the index passed to the method is not in range, then the remove() method is getting IndexError: pop index out of range.

Python Delete Element from List

In this program, we will delete elements using the del operator. The del operator removes the item or an element at the specified index location from the list, but the removed item is not returned, as it is with the pop() method. So essentially, this operator takes the item’s index to be removed as the argument and deletes the item at that index. Syntax: del list_name

# Python program to delete element from list by index

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed first item from the list
del my_list[0]

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]

The del operator will delete multiple items from the list using slicing. del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.

# Python program to delete item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed item from the list
del my_list[2:4]

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Javascript’]

Python Clear List

Python dictionary method clear() removes all the elements from the list. It clears the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty. The clear() method only empties the given list. Syntax: list_name.clear()

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed item from the list
my_list.clear()

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: []

Also See:- Remove Vowels from String in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *