Given a list of words in Python, the task is to remove the Nth occurrence of the given word in that list.
Examples:
Input: list - ["geeks", "for", "geeks"]
word = geeks, N = 2
Output: list - ["geeks", "for"]
Input: list - ["can", "you", "can", "a", "can" "?"]
word = can, N = 1
Output: list - ["you", "can", "a", "can" "?"]
Approach #1: By taking another list.
Make a new list, say newList. Iterate the elements in the list and check if the word to be removed matches the element and the occurrence number, otherwise, append the element to newList.
# Python3 program to remove Nth # occurrence of the given word # Function to remove Ith word def RemoveIthWord(lst, word, N): newList = [] count = 0 # iterate the elements for i in lst: if(i == word): count = count + 1 if(count != N): newList.append(i) else: newList.append(i) lst = newList if count == 0: print("Item not found") else: print("Updated list is: ", lst) return newList # Driver code list = ["geeks", "for", "geeks"] word = "geeks"N = 2 RemoveIthWord(list, word, N) |
Output :
Updated list is: ['geeks', 'for']
Approach #2: Remove from the list itself.
Instead of making a new list, delete the matching element from the list itself. Iterate the elements in the list and check if the word to be removed matches the element and the occurrence number, If yes delete that item and return true. If True is returned, print List otherwise, print “Item not Found”.
# Python3 program to remove Nth # occurrence of the given word # Function to remove Ith word def RemoveIthWord(list, word, N): count = 0 for i in range(0, len(list)): if (list[i] == word): count = count + 1 if(count == N): del(list[i]) return True return False # Driver code list = ['geeks', 'for', 'geeks'] word = 'geeks'N = 2 flag = RemoveIthWord(list, word, N) if (flag == True): print("Updated list is: ", list) else: print("Item not Updated") |
Output :
Updated list is: ['geeks', 'for']
Approach #3: Remove from the list using pop().
Instead of creating a new list and using if/else statement we can pop the matching element from the list using pop( ). We need to use an additional counter to keep track of the index.
Why we need an index ? because pop( ) needs index to pass inside i.e pop(index).
# Python3 program to remove Nth # occurrence of the given word # Function to remove nth word def omit(list1,word,n1): # for counting the occurence of word count=0 # for counting the index number # where we are at present index=0 for i in list1: index+=1 if i==word: count+=1 if count==n1: # (index-1) because in list # indexing start from 0th position list1.pop(index-1) return list1 # Driver code list1 = ["he", "is", "ankit", "is", "raj", "is","ankit raj"] word="is"n1=3 print("new list is :",omit(list1,word,n1)) |
Output :
new list is : ['he', 'is', 'ankit', 'is', 'raj', 'ankit raj']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Python program to read file word by word
- Python program to remove each y occurrence before x in List
- Python | Ways to find nth occurrence of substring in a string
- Python - Extract String after Nth occurrence of K character
- Python - Get Nth word in given String
- Python - Remove first occurrence of K in Tuple
- Python program to find occurrence to each character in given string
- Python program to find the occurrence of substring in the string
- Python - Remove Record if Nth Column is K
- Python | Get the string after occurrence of given substring
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Python | Program to implement Jumbled word game
- Python program for word guessing game
- Python program to capitalize the first and last character of each word in a string
- Python program to find the character position of Kth word from a list of strings
- Python Program to Return the Length of the Longest Word from the List of Words
- Generate two output strings depending upon occurrence of character in input string in Python
- Python | Last occurrence of some element in a list
- Python | Replace multiple occurrence of character by single
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ankitraj_iitb

