The Wayback Machine - https://web.archive.org/web/20241007014825/https://www.geeksforgeeks.org/python-list-remove/
Open In App

Python List remove() Method

Last Updated : 21 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python list remove() method removes a given element from the list.

Example:

Python3




lis = ['a', 'b', 'c']
lis.remove("b")
print(lis)


Output

['a', 'c']

List remove() Syntax

list_name.remove(obj) 

Parameter

  • obj: object to be removed from the list 

Returns

The method does not return any value but removes the given object from the list.

Exception

If the element doesn’t exist, it throws ValueError: list.remove(x): x not in list exception.

Note: It removes the first occurrence of the object from the list. 

What is Python list remove() Function

The list remove() function in Python removes the first occurrence of a given item from the list. It make changes to the current list.

It only takes one argument that is the element you want to remove and if that element is not present in the list, it gives ValueError.

It is very useful in removing incorrect values from a list, without affecting the rest of the list.

How to Use List remove() Method

remove() function is a very important function of lists. It is very easy to use, just call the function with a list object and pass the value you want to remove as a parameter. Let’s look at an example to understand how to remove an element from a list:

More Examples of list remove()

Let’s see some of the most common use-case scenarios with the list remove() function to have a deep understanding of the topic.

  • Remove an element from the list 
  • Deleting Element that doesn’t Exist
  • Remove Duplicates from List in Python
  • Remove all Occurrences of a value from a List
  • Removing a nested list element from a list
  • Removing elements from a list based on a condition
  • Removing an Element by Value from a List
  • Removing elements from a list using the Filter function

1. Remove an element from the list in Python

In this example, we are showing how we can use the remove() function with the Python list. Remove function removes the specified element’s first occurrence in the list.

Python3




# the first occurrence of 1 is removed from the list
list1 = [ 1, 2, 1, 1, 4, 5 ]
list1.remove(1)
print(list1)
 
# removes 'a' from list2
list2 = [ 'a', 'b', 'c', 'd' ]
list2.remove('a')
print(list2)


Output

[2, 1, 1, 4, 5]
['b', 'c', 'd']

Time complexity: for the first list removal: O(n)
Time complexity: for the second list removal: O(1)
Space complexity: O(1) for both cases.

2. Deleting Element that doesn’t Exist

In this example, we are removing the element  â€˜e’ which does not exist.

Python3




# removes 'e' from list2
list2 = [ 'a', 'b', 'c', 'd' ]
 
list2.remove('e')
print(list2)


Output

Traceback (most recent call last):
File "/home/e35b642d8d5c06d24e9b31c7e7b9a7fa.py", line 8, in
list2.remove('e')
ValueError: list.remove(x): x not in list

3. Remove Duplicates from the List in Python

In this example, we are removing the element which comes multiple times in the list.

Python3




# My List
list2 = [ 'a', 'b', 'c', 'd', 'd', 'e', 'd' ]
 
# removing 'd'
list2.remove('d')
 
print(list2)


Output

['a', 'b', 'c', 'd', 'e', 'd']

Time complexity: O(n) 
Space complexity: O(1)

Note: If a list contains duplicate elements, it removes the first occurrence of the object from the list. 

4. Remove all Occurrences of a value from a List

Let’s see how to remove all the occurrences of a value from a list.

Example 1: Remove all the 1’s from the list and print the list.

In this example, we remove 1 from the list until all 1 is removed.

Python3




list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5]
 
# looping till all 1's are removed
while (list1.count(1)):
    list1.remove(1)
     
print(list1)


Output

[2, 3, 4, 4, 5]

Time complexity: O(n^2) 
Space complexity: O(1)

Example 2: Given a list, remove all the 2’s from the list using in keyword 

In this example, we remove 2 from the list until all the 2 are removed.

Python3




mylist = [1, 2, 3, 2, 2]
 
# looping till all 2's are removed
while 2 in mylist:
    mylist.remove(2)
 
print(mylist)


Output

[1, 3]

Time complexity: O(n^2)
Space complexity: O(1) – The space complexity is O(1) since we are only modifying the existing list and not creating any additional data structures proportional to the input size.

5. Removing a nested list element from a list

In this example, we are removing a list from a 2d List.

Python3




data = [[1, 2], [3, 4], [5, 6]]
data.remove([3, 4])
print(data)  # Output: [[1, 2], [5, 6]]


Output

[[1, 2], [5, 6]]

Time complexity: O(n) 
Space complexity: O(1)

6. Removing elements from a list based on a condition using a list comprehension

In this example, we are moving numbers from the list using list comprehension.

Python3




numbers = [1, 2, 3, 4, 5]
numbers = [x for x in numbers if x != 3]
print(numbers)


Output

[1, 2, 4, 5]

Time complexity: O(n) 
Space complexity: O(1)

7. Removing an Element by Value from a List

This example demonstrates how to remove a specific element (in this case, ‘3’) from a list using the remove() method. It first checks if the element exists in the list before attempting to remove it.

Python3




my_list = [1, 2, 3, 4, 5]
 
# Remove element '3' from the list
if 3 in my_list:
    my_list.remove(3)
 
print("Updated list:", my_list)


Output

Updated list: [1, 2, 4, 5]

Time complexity: O(n) 
Space complexity: O(1)

8. Removing elements from a list using the Filter function

In this example, we are using the lambda function to check the condition and filter out the data from the list.

Python3




numbers = [1, 2, 3, 4, 5]
numbers = list(filter(lambda x: x != 3, numbers))
print(numbers)


Output

[1, 2, 4, 5]

Time complexity: O(n) 
Space complexity: O(n)

We have discussed the definition, uses and examples of the list remove() method in Python. The list remove function is an important list operating function and is used to remove a particular value from the list.

Read More List Methods

Similar Reads:



Previous Article
Next Article

Similar Reads

Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
Python Remove Duplicates from a List
The job is simple. We need to take a list, with duplicate elements in it and generate another list that only contains the element without the duplicates in them. Examples: Input : [2, 4, 10, 20, 5, 2, 20, 4] Output : [2, 4, 10, 20, 5] Input : [28, 42, 28, 16, 90, 42, 42, 28] Output : [28, 42, 16, 90] We can use not in on list to find out the duplic
3 min read
Python | Remove empty tuples from a list
In this article, we will see how can we remove an empty tuple from a given list of tuples. We will find various ways, in which we can perform this task of removing tuples using various methods and ways in Python. Examples: Input : tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('',''),()]Output : [('ram', '15',
8 min read
Python | Remove and print every third from list until it becomes empty
Given a list of numbers, Your task is to remove and print every third number from a list of numbers until the list becomes empty. Examples: Input : [10, 20, 30, 40, 50, 60, 70, 80, 90] Output : 30 60 90 40 80 50 20 70 10 Explanation: The first third element encountered is 30, after 30 we start the count from 40 for the next third element which is 6
4 min read
Ways to remove particular List element in Python
List is an important container and is used almost in every code of day-day programming as well as web development. The more it is used, more is the required to master it and hence knowledge of its operations is necessary. Let's see the different ways of removing particular list elements. Method #1 : Using remove() remove() can perform the task of r
6 min read
Python | Remove empty strings from list of strings
In many scenarios, we encounter the issue of getting an empty string in a huge amount of data and handling that sometimes becomes a tedious task. Let's discuss certain way-outs to remove empty strings from list of strings. Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this
7 min read
Python | Remove consecutive duplicates from list
In Python, we generally wish to remove the duplicate elements, but sometimes for several specific usecases, we require to have remove just the elements repeated in succession. This is a quite easy task and having a shorthand for it can be useful. Let's discuss certain ways in which this task can be performed. Method #1 : Using groupby() + list comp
4 min read
Python | Remove square brackets from list
Sometimes, while working with displaying the contents of list, the square brackets, both opening and closing are undesired. For this when we need to print the whole list without accessing the elements for loops, we require a method to perform this. Let's discuss a shorthand by which this task can be performed. Method 1: Using str() + list slicing T
5 min read
Python - Remove Key from Dictionary List
Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove a specific key from a dictionary list. This kind of problem is very common and has application in almost all domains including day-day programming and web development domain. Let's discuss certain ways in which this task can be performed. Method #1 :
7 min read
Python - Remove empty value types in dictionaries list
Sometimes, while working with Python dictionaries, we require to remove all the values that are virtually Null, i.e does not hold any meaningful value and are to be removed before processing data, this can be an empty string, empty list, dictionary, or even 0. This has applications in data preprocessing. Let us discuss certain ways in which this ta
7 min read
Python | Remove Redundant Substrings from Strings List
Given list of Strings, task is to remove all the strings, which are substrings of other Strings. Input : test_list = ["Gfg", "Gfg is best", "Geeks", "for", "Gfg is for Geeks"] Output : ['Gfg is best', 'Gfg is for Geeks'] Explanation : "Gfg", "for" and "Geeks" are present as substrings in other strings.Input : test_list = ["Gfg", "Geeks", "for", "Gf
5 min read
Remove falsy values from a list in Python
Prerequisite: Truthy vs Falsy Values in Python In Python, the value that evaluates to False is considered as a Falsy value. The value such as empty list, empty dictionary, empty tuple, empty set, empty string, None, False, 0 are considered as Falsy values. So our task is to remove all the Falsy values from the List. Examples: Input: [10,20,30,0,Fal
3 min read
Remove all Alphanumeric Elements from the List - Python
Alphanumeric elements consist of only alphabetical and numerical characters. Any of the special characters are not included in alphanumeric elements. This article is going to show you multiple ways to remove all the alphanumeric elements from a List of strings using Python. Example 1: Input: ['a','b', '!', '1', '2', '@', 'abc@xyz.com'] Output: ['!'
2 min read
Remove Last Element from List in Python
Given a list, the task is to write a Python program to remove the last element present in the list and update the original list 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 list and print the resultant
4 min read
Remove all the occurrences of an element from a list in Python
The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Example Input1: 1 1 2 3 4 5 1 2 1 Output1: 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [2, 3, 4, 5, 2]Remove all Occurrences of an Item fr
4 min read
How to Remove an Item from the List in Python
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:
6 min read
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
Python - Ways to remove duplicates from list
This article focuses on one of the operations of getting a unique list from a list that contains a possible duplicate. Removing duplicates from list operation has a large number of applications and hence, its knowledge is good to have in Python. Ways to Remove duplicates from the list:Below are the methods that we will cover in this article: Using
10 min read
Python - Remove first element of list
The queue data structure is a very well-known data structure, lists in Python usually append the elements to the end of the Python list. For implementing a queue data structure, it is essential to be able to remove the front element from a list. Let's discuss the ways of removing the first element of the list. Method 1: Remove Elements From Lists i
7 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python groupby method to remove all consecutive duplicates
Given a string S, remove all the consecutive duplicates. Examples: Input : aaaaabbbbbb Output : ab Input : geeksforgeeks Output : geksforgeks Input : aabccba Output : abcba We have existing solution for this problem please refer Remove all consecutive duplicates from the string link. We can solve this problem in python quickly using itertools.group
2 min read
Python | os.remove() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have
3 min read
Python Set - remove() method
Python remove() Function is a built-in method to remove elements from the set. remove() method takes exactly one argument. Syntax set.remove(element) If the element passed to the remove() is present in the set then the element will be removed from the set. If the element passed to the remove() is not present in the set then KeyError Exception will
1 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Python | Remove all duplicates words from a given sentence
Given a sentence containing n words/strings. Remove all duplicates words/strings which are similar to each others. Examples: Input : Geeks for Geeks Output : Geeks for Input : Python is great and Java is also great Output : is also Java Python and great We can solve this problem quickly using python Counter() method. Approach is very simple. 1) Spl
7 min read
How to Remove Letters From a String in Python
Strings are data types used to represent text/characters. In this article, we present different methods for the problem of removing the ith character from a string and talk about possible solutions that can be employed in achieving them using Python. Input: 'Geeks123For123Geeks'Output: GeeksForGeeksExplanation: In This, we have removed the '123' ch
6 min read
Remove Special Characters from String Python
The generic problem faced by the programmers is to remove unwanted characters from a string using Python. But sometimes the requirement is way above and demands the removal of more than 1 character, but a list of such malicious characters. These can be in the form of special characters for reconstructing valid passwords and many other applications
6 min read
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 | Remove all sublists outside the given range
Given, list of lists and a range, the task is to traverse each sub-list and remove sublists containing elements falling out of given range. Examples: Input : left= 10, right = 17, list = [[0, 1.2, 3.4, 18.1, 10.1], [10.3, 12.4, 15, 17, 16, 11], [100, 10, 9.2, 11, 13, 17.1], ] Output: [[10.3, 12.4, 15, 17, 16, 11]] Input : left= 1, right = 9, list =
3 min read
Python | Remove Consecutive tuple according to key
Sometimes, while working with Python list, we can have a problem in which we can have a list of tuples and we wish to remove them basis of first element of tuple to avoid it's consecutive duplication. Let's discuss certain way in which this problem can be solved. Method : Using groupby() + itemgetter() + next() This task can be performed using comb
5 min read