list1 = [5, 20, 15, 20, 25, 50, 20]
# list comprehension
# remove specific items and return a new list
def remove_value(sample_list, val):
return [i for i in sample_list if i != val]
res = remove_value(list1, 20)
print(res)
Solution2:while loop (slow solution)
list1 = [5, 20, 15, 20, 25, 50, 20]
while 20 in list1:
list1.remove(20)
print(list1)
Solution1:Use the list comprehension
Solution2:while loop (slow solution)
need an explanation for this answer? contact us directly to get an explanation for this answer