Lists are ordered sequences that can contain a wide range of object types. Members on lists can also be duplicated. Lists in Python are equivalent to arrays in other programming languages. There is, however, one significant difference. Arrays can only have elements of the same data type, whereas Python lists can have items of different data types.
Python Lists have several methods for removing elements from the list.
Examples:
Input:
givenlist=["hello", "this", "is", "Btech", "Geeks" ,"is" ] , value=is
Output:
["hello", "this", "Btech", "Geeks"]
Remove Elements from the List by Values
There are several ways to remove elements from the list by values some of them are:
- Using remove() function
- Removing given value using remove() if exist
- Removing all occurrences of given value
- Using values, remove all occurrences of multiple elements from a list.
Method #1:Using remove() function
The remove method removes a list element based on its value rather than its index. When more than one element matches the provided value, the first matching element (the one with the lowest index) is removed. This is useful when you know which element needs to be removed ahead of time and don’t want to waste time looking for its index before removing it.
Let us now use the remove() function to remove the first instance of an element ‘is’.
Below is the implementation:
# Function which removes the element for given value and returns list def removeValue(givenlist, value): # removing the value using remove() givenlist.remove(value) # return the list return givenlist # Driver code # given list givenlist = ["hello", "this", "is", "Btech", "Geeks", "is"] # given value which is to be removed value = 'is' # passing list and value to removeValue function to remove the element print(removeValue(givenlist, value))
Output:
['hello', 'this', 'Btech', 'Geeks', 'is']
If we use the remove() function to remove an element from a list that does not exist, what happens? In that case, Value Error will be returned.
Method #2: Removing given value using remove() if exist
To avoid the ValueError, we check to see if the value is already in the list and then remove it.
Below is the implementation:
# Function which removes the element for given value and returns list def removeValue(givenlist, value): # checking if value is present in givenlist if value in givenlist: # removing the value using remove() givenlist.remove(value) # return the list return givenlist # Driver code # given list givenlist = ["hello", "this", "is", "Btech", "Geeks", "is"] # given value which is to be removed value = 'is' # passing list and value to removeValue function to remove the element print(removeValue(givenlist, value))
Output:
['hello', 'this', 'Btech', 'Geeks', 'is']
Method #3:Removing all occurrences of given value
As we saw in the previous examples, the remove() function always deletes the first occurrence of the given element from the list. To delete all occurrences of an element, we must use the remove() function in a loop until all occurrences are gone.
So we use while loop to achieve that.
Below is the implementation:
# Function which removes the element for given value and returns list def removeValue(givenlist, value): # using while loop to remove all occurrences of given value while(value in givenlist): # removing the value using remove() givenlist.remove(value) # return the list return givenlist # Driver code # given list givenlist = ["hello", "this", "is", "Btech", "Geeks", "is"] # given value which is to be removed value = 'is' # passing list and value to removeValue function to remove the element print(removeValue(givenlist, value))
Output:
['hello', 'this', 'Btech', 'Geeks']
Method #4: Using values, remove all occurrences of multiple elements from a list.
Assume we have a list of numbers and another list of values that we want to remove from the first list. We want to remove all of the elements from list2 from list1.
We’ve created a separate function for this, which takes two different lists as arguments.
- The first is the givenlist.
- The elements we want to remove are listed in the second list(valuelist)
It deletes all occurrences from the original list for each element in the second list.
Approach:
- Traverse the value list
- Using while loop remove all occurrences of the value from given list.
- Return given list
Below is the implementation:
# Function which removes the element for given value and returns list def removeValue(givenlist, valuelist): # Traverse the value list for value in valuelist: # using while loop to remove all occurrences of given value while(value in givenlist): # removing the value using remove() givenlist.remove(value) # return the list return givenlist # Driver code # given list givenlist = ["hello", "this", "is", "Btech", "Geeks", "is"] # given value list which is to be removed valuelist = ['is', 'hello', 'this'] # passing list and valuelist to removeValue function to remove the element print(removeValue(givenlist, valuelist))
Output:
['Btech', 'Geeks']
Related Programs:
- python remove elements from list by index or indices
- python how to remove multiple elements from list
- delete elements from a numpy array by value or conditions in python
- how to remove elements from a list while iterating in c
- python remove elements from a list while iterating
- how to remove elements from a list based on the given condition
- remove duplicate elements from a sorted singly linked list