EXPERIMENT-10
Objective: To make a program to remove i’th occurrence of a given word.
Source code:
def remove_ith_occurrence(word_list,word_to_remove,i):
count=0
for index,word in enumerate(word_list):
if word == word_to_remove:
count+=1
if count == i:
del word_list[index]
return word_list #stop after removing ith occurrence
return word_list
words= ['apple','banana','cherry','apple','date','apple']
print(words)
word_to_remove='apple'
i=2
result= remove_ith_occurrence(words,word_to_remove,i)
print(result)
Input and output:
Conclusion: The experiment successfully developed a program to remove the i’th occurrence
of a given word from a text. The program accurately identifies and eliminates the desired
occurrence.