Using ‘while’ loop, we can delete all the instances of a value from the list.
Example
def remove_element(item, list):
while(item in list):
list.remove(item)
remove_elements.py
def remove_element(item, list):
while(item in list):
list.remove(item)
data = [1, 2, 3, 4, 5, 3, 2, 2, 3, 4, 3]
print(f"data : {data}")
print("\nRemoving 3 from list")
remove_element(3, data)
print(f"\ndata : {data}")
Output
$python3 remove_elements.py
data : [1, 2, 3, 4, 5, 3, 2, 2, 3, 4, 3]
Removing 3 from list
data : [1, 2, 4, 5, 2, 2, 4]
Previous Next Home
No comments:
Post a Comment