Python program to remove an element from a list
Python List remove()In this tutorial, we will learn about the Python List remove() method with the help of examples. Show
The remove() method removes the first matching element (which is passed as an argument) from the list. Example# create a list prime_numbers = [2, 3, 5, 7, 9, 11]# remove 9 from the list
prime_numbers.remove(9)
# Updated prime_numbers List
print('Updated List: ', prime_numbers)
# Output: Updated List: [2, 3, 5, 7, 11]How to remove an element from a list in PythonPython provides the following methods to remove one or multiple elements. We can delete the elements using the del keyword by specifying the index position. Let's understand the following methods.
The remove() methodThe remove() method is used to remove the specified value from the list. It accepts the item value as an argument. Let's understand the following example. Example - Output: The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Bob', 'Charlie', 'Bob', 'Dave']
If the list contains more than one item of the same name, it removes that item's first occurrence. Example - Output: The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Joseph', 'Charlie', 'Bob', 'Dave']
The pop() methodThe pop() method removes the item at the specified index position. If we have not specified the index position, then it removes the last item from the list. Let's understand the following example. Example - Output: The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Joseph', 'Bob', 'Charlie', 'Dave']
After removing element: ['Joseph', 'Bob', 'Charlie']
We can also specify the negative index position. The index -1 represents the last item of the list. Example - Output: The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Joseph', 'Bob', 'Charlie', 'Dave']
The clear() methodThe clear() method removes all items from the list. It returns the empty list. Let's understand the following example. Example - Output: [10, 20, 30, 40, 50, 60]
[]
The del statementWe can remove the list item using the del keyword. It deletes the specified index item. Let's understand the following example. Example - Output: [10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50]
[10, 20, 30, 40]
It can delete the entire list. Output: Traceback (most recent call last):
File "C:/Users/DEVANSH SHARMA/PycharmProjects/Practice Python/first.py", line 14, in
print(list1)
NameError: name 'list1' is not defined
We can also delete the multiple items from the list using the del with the slice operator. Let's understand the following example. Example - Output: [10, 20, 30, 40, 50, 60]
[10, 40, 50, 60]
[60]
[]
Using List ComprehensionThe list comprehension is slightly different way to remove the item from the list. It removes those items which satisfy the given condition. For example - To remove the even number from the given list, we define the condition as i % 2 which will give the reminder 2 and it will remove those items that's reminder is 2. Let's understand the following example. Example - Output: [20, 34, 40, 60]
[11, 45]
Next TopicHow to Round number in Python ← prev next → Remove multiple elements from a list in PythonGiven a list of numbers, write a Python program to remove multiple elements from a list based on the given condition. Example: Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17, 18, 23, 50] Output: Remove = [1:5], New_list = [11, 50]Multiple elements can be deleted from a list in Python, based on the knowledge we have about the data. Like, we just know the values to be deleted or also know the indexes of those values. Let’s see different examples based on different scenario. Example #1: Let’s say we want to delete each element in the list which is divisible by 2 or all the even numbers. Python3
Output: New list after removing all even numbers: [11, 5, 17, 23] Example #2: Using list comprehension Python3
Output: 11 5 17 23Example #3: Remove adjacent elements using list slicing Python3
Output: 11 50Example #4: Using list comprehension Python3
Output: New list after removing unwanted numbers: [17, 18, 23, 50]Example #5: When index of elements is known. Python3
Output: 5 17 50
Article Tags :
Python Python Programs School Programming
Python list-programs python-list Practice Tags :
python-list
Read Full Article
Python | Remove given element from the listGiven a list, write a Python program to remove the given element (list may have duplicates) from given list. There are multiple ways we can do this task in Python. Let’s see some of Pythonic ways to do this task. Method #1: Using pop() method [Remove given element found first.]
Output:
original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 9, 2, 9]
Output: original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 9, 2, 9] Now, let’s see the ways to remove all occurrence of given element. Method #3: Using set Since the list is converted to set, all duplicates are removed, but the ordering of list cannot be preserved.
Output:
original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [8, 1, 2, 4]
Output:
original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 2]
Article Tags :
Python Python Programs
Python list-programs
Read Full Article
Python remove() methodPython removes () method is a built-in method available with the list. It helps to remove the given very first element matching from the list. Syntax:list.remove(element)The element that you want to remove from the list. ReturnValue There is no return value for this method. Tips for using remove() method:Following are the important points to remember when using remove () method:
Example: Using remove() method to remove an element from the listHere is a sample list that i have my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya. my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove(12) # it will remove the element 12 at the start. print(my_list) my_list.remove('Riya') # will remove the first Riya from the list print(my_list) my_list.remove(100) #will throw an error print(my_list)Output: ['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback (most recent calllast): File "display.py", line 9, inPython - Remove List Items
❮ Previous
Next ❯
Remove Specified ItemThe remove() method removes the specified item. ExampleRemove "banana":
thislist = ["apple", "banana", "cherry"]
Try it Yourself »
Remove an item from a list in Python (clear, pop, remove, del)Posted: 2019-05-29 / Modified: 2021-04-06 / Tags: Python, List Tweet In Python, use list methods clear(), pop(), and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.
See the following article for adding items to the list.
Sponsored Link |