Python Program to Remove the ith Occurrence of the Given Word in a List
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=input("Enter element" + str(x+1) + ":")
a.append(element)
print(a)
c=[]
count=0
b=input("Enter word to remove: ")
n=int(input("Enter the occurrence to remove: "))
for i in a:
if(i==b):
count=count+1
if(count!=n):
c.append(i)
else:
c.append(i)
if(count==0):
print("Item not found ")
else:
print("The number of repetitions is: ",count)
print("Updated list is: ",c)
print("The distinct elements are: ",set(a))
Output
Enter the number of elements in list:5
Enter element1:"apple"
Enter element2:"apple"
Enter element3:"ball"
Enter element4:"ball"
Enter element5:"cat"
['apple', 'apple', 'ball', 'ball', 'cat']
Enter word to remove: "ball"
Enter the occurence to remove: 2
('The number of repitions is: ', 2)
('Updated list is: ', ['apple', 'apple', 'ball', 'cat'])
('The distinct elements are: ', set(['ball', 'apple', 'cat']))
