Python Program to Remove the ith Occurrence of the Given Word in a List where Words can Repeat

This is a Python Program to remove the ith occurrence of the given word in list where words can repeat.

Problem Description

The program takes a list and removes the ith occurrence of the given word in the list where words can repeat.

Problem Solution

1. Take the number of elements in the list and store it in a variable.
2. Accept the values into the list using a for loop and insert them into the list.
3. Use a for loop to traverse through the elements in the list.
4. Then use an if statement to check if the word to be removed matches the element and the occurrence number and otherwise it appends the element to another list.
5. The number of repetitions along with the updated list and distinct elements is printed.
6. Exit.

advertisement

Program/Source Code

Here is source code of the Python Program to remove the ith occurrence of the given word in list where words can repeat. The program output is also shown below.

1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values of elements into the list.
3. The append function obtains each element from the user and adds the same to the end of the list as many times as the number of elements taken.
4. User must enter the word and the occurrence of the word to remove.
5. A for loop is used to traverse across the elements in the list.
6. An if statement then checks whether the element matches equal to the word that must be removed and whether the occurrence of the element matches the occurrence to be removed.
7. If both aren’t true, the element is appended to another list.
8. If only the word matches, the count value is incremented.
9. Finally the number of repetitions along with the updated list and the distinct elements is printed.