Python Program to Remove the ith Occurrence of the Given Word in a List

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.

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.

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))
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
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']))
 
Case 2:
Enter the number of elements in list:6
Enter element1:"A"
Enter element2:"B"
Enter element3:"A"
Enter element4:"A"
Enter element5:"C"
Enter element6:"A"
['A', 'B', 'A', 'A', 'C', 'A']
Enter word to remove: "A"
Enter the occurence to remove: 3
('The number of repitions is: ', 4)
('Updated list is: ', ['A', 'B', 'A', 'C', 'A'])
('The distinct elements are: ', set(['A', 'C', 'B']))

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.