Here, we are going to actualize a Python program that will evacuate all events a given component from the rundown.
Given a rundown, and we need to expel all events of a given component from the rundown in Python.
Input: list = [10, 20, 10, 30, 10, 40, 10, 50] n = 10 Output: list after removing 10 from the list list = [20, 30, 40, 50]
- Run some time circle from 0th component to last component’s file.
- Check the component whether it is equivalent to the number (which is to be expelled) or not.
- On the off chance that any component of the rundown is equivalent to the number (which is to be evacuated), expel that component from the rundown.
- To expel the number from the rundown, use list.remove() technique.
- In the wake of expelling the number/component from the rundown, decline the length, since one thing is erased, and afterwards proceed with the circle to check the following thing at same record (on the grounds that in the wake of evacuating the component – next components movements to the past list.
- On the off chance that component isn’t found (for example isn’t expelled), at that point increment the circle counter to check next component.
# list with integer elements list = [10, 20, 10, 30, 10, 40, 10, 50] # number (n) to be removed n = 10 # print original list print ("Original list:") print (list) # loop to traverse each element in list # and, remove elements # which are equals to n i=0 #loop counter length = len(list) #list length while(i<length): if(list[i]==n): list.remove (list[i]) # as an element is removed # so decrease the length by 1 length = length -1 # run loop again to check element # at same index, when item removed # next item will shift to the left continue i = i+1 # print list after removing given element print ("list after removing elements:") print (list)
Original list: [10, 20, 10, 30, 10, 40, 10, 50] list after removing elements: [20, 30, 40, 50]