- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat
When it is required to remove a specific occurrence of a given word in a list of words, given that the words can be repeated, a method can be defined, that iterates through the list, and increments the counter by 1. If the count and the specific occurrence match, then the specific element from the list can be deleted.
Below is a demonstration of the same −
Example
def remove_word(my_list, my_word, N): count = 0 for i in range(0, len(my_list)): if (my_list[i] == my_word): count = count + 1 if(count == N): del(my_list[i]) return True return False my_list = ['Harry', 'Jane', 'Will', 'Rob', 'Harry'] print("The list is :") print(my_list) my_word = 'Harry' N = 2 flag_val = remove_word(my_list, my_word, N) if (flag_val == True): print("The updated list is: ", my_list) else: print("Item hasn't been updated")
Output
The list is : ['Harry', 'Jane', 'Will', 'Rob', 'Harry'] The updated list is: ['Harry', 'Jane', 'Will', 'Rob']
Explanation
A method named ‘remove_word’ is defined, which takes the list, a word, and a value for ‘n’ as a parameter.
A ’count’ value is initialized to 0.
The list is iterated over, and it is checked to see if the element in the list matches a specific word.
If they match, the count variable is incremented.
If this count variable is equal to a value ‘n’, then the element from the list is deleted.
It is used using the ‘del’ keyword.
A list of strings is defined and displayed on the console.
The method is called by passing relevant parameters.
The output is displayed on the console.
- Related Articles
- Program to remove last occurrence of a given target from a linked list in Python
- Python program to find word score from list of words
- Python Program to return the Length of the Longest Word from the List of Words
- Python program to remove each y occurrence before x in List
- C# program to remove the first occurrence of a node in a Linked List
- Python program to remove all duplicates word from a given sentence.
- Program to check a string can be broken into given list of words or not in python
- How to find the nth occurrence of substring in a string in Python?
- Remove Nth Node From End of List in Python
- Program to Find Out the Occurrence of a Digit from a Given Range in Python
- Program to check we can spell out the target by a list of words or not in Python
- Python Program to Print Nth Node from the last of a Linked List
- Program to reverse the position of each word of a given string in Python
- Python Program to Remove the nth Index Character from a Non-Empty String
- Program to find the largest grouping of anagrams from a word list in Python
