Open In App

How to Remove an Item from the List in Python

Last Updated : 21 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python Lists have various built-in methods to remove items from the list. Apart from these, we can also use different methods to remove an element from the list by specifying its position. This article will examine various Python methods for removing items from lists.

Example

Input: ['Rose',' Lily', 'Lotus', 'Sun', 'Sunflower']
Delete: 'Sun'
Output: ['Rose',' Lily', 'Lotus', 'Sunflower']
Explanation: In this, we have removed the 'Sun' element from the given list.

Remove an Item from a List

We will use a different method to Remove Elements from the List in Python:

1. Remove Elements from the List using remove()

We can remove elements from the list by passing the value of the item to be deleted as the parameter to remove the () function.

Python3




lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
    'Lily', 'Carnations']
print("Original List is :", lst)
 
# using remove()
lst.remove('Orchids')
print("After deleting the item :", lst)


Output

Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']

2. Remove Element from the List using del()

We can remove elements from the list using Del(). The Python del statement is not a function of List. Items of the list can be deleted using the del statement by specifying the index of the item (element) to be deleted.

Python3




lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
    'Lily', 'Carnations']
print("Original List is :", lst)
 
# using del statement
# to delete item (Orchids at index 1)
# from the list
del lst[1]
print("After deleting the item :", lst)


Output

Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']

3. Remove Element from the List using List Comprehension

We can remove elements from the list while iterating. In this method, we are using list comprehension. Here, we are appending all the elements except the elements that have to be removed.

Python3




# Python program to remove given element from the list
list1 = [1, 9, 8, 4, 9, 2, 9]
     
# Printing initial list
print ("original list : "+ str(list1))
 
# using List Comprehension
# to remove list element 9
list1 = [ele for ele in list1 if ele != 9]
     
# Printing list after removal
print ("List after element removal is : " + str(list1))


Output

original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 2]

4. Remove Element from the List using pop() 

We can remove elements from the list using pop(). The pop() is also a method of listing. We can remove the element at the specified index and get the value of that element using pop()

Python3




lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
    'Lily', 'Carnations']
print("Original List is :", lst)
 
# using pop() to delete item
# ('Orchids' at index 1) from the list
a = lst.pop(1)
print("Item popped :", a)
print("After deleting the item :", lst)


Output

Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
Item popped : Orchids
After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']

5. Remove Element from the List Using discard()

We can remove elements from the list using discard(). In this method, we convert a list into a set and then delete an item using the discard() function. Then we convert the set back to the list.

Python3




# Python program to remove given element from the list
lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
    'Lily', 'Carnations']
print("Original List is :", lst)
 
# using discard() method to remove list element 'orchids'
lst = set(lst)
lst.discard('Orchids')
 
# Converting set back to list
lst=list(lst)
 
print("List after element removal is :", lst)


Output:

Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
List after element removal is : ['Lily', 'Carnations', 'Iris', 'Rose', 'Lavender']

Note: Since the list is converted to a set, all duplicates will be removed and the ordering of the list cannot be preserved.

6. Remove Element From the List Using filter()

We can remove elements from the list using filter(). In this method, we filter the unwanted element from the list using the filter() function. 

Python3




# Python program to remove given element from the list
lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
    'Lily', 'Carnations']
print("Original List is :", lst)
 
# using discard() method to remove list element 'orchids'
lst1 = filter(lambda item: item!='Orchids',lst)
 
print("List after element removal is :", list(lst1))


Output

Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations']
List after element removal is : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']

7. Remove Element from the List Using Slicing

We can remove elements from the list using slicing. This method creates a new list by slicing the original list and concatenating the parts that do not include the removed element.

Python3




my_list = [1, 2, 3, 4, 5]
my_list = my_list[:2] + my_list[3:]
print(my_list)  # Output: [1, 2, 4, 5]


Output:

[1, 2, 4, 5]

8. Remove Element from the List Using Itertools

We can remove elements from the list using itertools. The code uses the itertools.filterfalse() function to remove all occurrences of the number 9 from a given list.

It creates a lambda function to check if an element is equal to 9 and applies the filter to the list. The resulting filtered list is printed as the output.

Python3




import itertools
 
lst = [1, 9, 8, 4, 9, 2, 9]
print("Original List is :", lst)
 
# itertools.filterfalse() to filter out all occurrences of 9 from the list
lst_filtered = list(itertools.filterfalse(lambda x: x == 9, lst))
print("List after element removal is :", lst_filtered)
#this code is contributed by Jyothi pinjala.


Output

Original List is : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 2]

In this article, we have discussed various methods to remove an item from the list. There are a total of 8 methods mentioned in this article. Removing an element from lists can be done using built-in functions but we have also used non-conventional methods.

Similar Reads:



Previous Article
Next Article

Similar Reads

Python | Remove item from dictionary when key is unknown
Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning. Let's discuss the various ways to remove items from the dictionary when key is unknown. Method #1 : Using n
6 min read
Python program to Remove the last item from array
Given an array, the task is to write a Python program to remove the last element in Python. Example: Input: ["geeks", "for", "geeks"] Output: ["geeks", "for"] Input: [1, 2, 3, 4, 5] Output: [1, 2, 3, 4] Explanation: Here simply we have to remove the last element present in the array and return the remaining array. Note: Arrays are not supported in
4 min read
Python Remove Item from Dictionary by Value
Python, with its elegant syntax and powerful data structures, stands out as one of the most favored programming languages. Among its arsenal of data structures, the dictionary is a versatile and indispensable tool for developers. Dictionaries facilitate the storage of key-value pairs, allowing for efficient data retrieval and manipulation. In this
3 min read
Python Remove Item from Dictionary by Key
A dictionary in Python is a mutable and dynamic data type that provides a flexible way to access and manipulate data. As distinct from a list or a tuple, where elements are accessed via indices, a dictionary leverages a unique key representing each item. In this article, we will see how to remove items from the dictionary by key in Python. Remove I
3 min read
Appending Item to Lists of list using List Comprehension | Python
If you are a Python user, you would know that in Python, we can use the append() method to add an item to an existing list. This list may already contain other items or be empty. Further, the item to be added can simply be a number a character, or even an entire tuple or list. However, if you are trying to append an item to lists within a list comp
5 min read
Python program to sort a list of tuples by second Item
Given a list of tuples, write a Python program to sort the tuples by the second item of each tuple. Examples: Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)] Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)] Input : [('452', 10), ('256', 5), ('100', 20), ('135', 15)] Output : [('256', 5), ('452', 10), ('135', 15), ('100', 20)] Method #1: Using
6 min read
Python | Inserting item in sorted list maintaining order
Working with sorted list is essential because mostly the data we get is ordered. Any query can come to insert the new data into its appropriate position. Hence requires to know how to perform these dynamic queries. Lets discuss certain ways in which this can be performed. Method #1 : Naive Method In this method, we just simply test for the value an
6 min read
Python | Find mismatch item on same index in two list
Given two list of integers, the task is to find the index at which the element of two list doesn't match. Input: Input1 = [1, 2, 3, 4] Input2 = [1, 5, 3, 6] Output: [1, 3] Explanation: At index=1 we have 2 and 5 and at index=3 we have 4 and 6 which mismatches. Below are some ways to achieve this task. Method #1: Using Iteration C/C++ Code # Python
4 min read
Python - Group single item dictionaries into List values
Given a List of single-item dictionaries, group them into dictionary value lists according to similar values. Input : [{"Gfg" : 3}, {"is": 8}, {"Gfg": 18}, {"Best": 33}]Output : {'Gfg': [3, 18], 'is': [8], 'Best': [33]} Explanation : Each key converted to list values and dictionary. Input : [{"Gfg" : 3}, {"Gfg": 8}, {"Gfg": 18}, {"Best": 33}] Outpu
6 min read
Python - Sort list of Single Item dictionaries according to custom ordering
Given single item dictionaries list and keys ordering list, perform sort of dictionary according to custom keys. Input : test_list1 = [{'is' : 4}, {"Gfg" : 10}, {"Best" : 1}], test_list2 = ["Gfg", "is", "Best"] Output : [{'Gfg': 10}, {'is': 4}, {'Best': 1}] Explanation : By list ordering, dictionaries list get sorted. Input : test_list1 = [{"Gfg" :
4 min read
Practice Tags :