Program to remove the i th occurrence of the given word in a list where words repeat

Python program to remove Nth occurrence of the given word

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" "?"]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.


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




# 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




# 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




# Python3 program to remove Nth
# occurrence of the given word
# Function to remove nth word
def omit[list1,word,n1]:
# for counting the occurrence 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. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
Read Full Article

Python program to remove nth occurrence of the given word list

  • To allow the user to input a number of elements in the list and store it in a variable.
  • Accept the values into the list using a for loop and insert them into the python list.
  • Use a for loop to traverse through the elements in the list.
  • 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.
  • The number of repetitions along with the updated list and distinct elements is printed.
# python program to remove nth occurrence of the given word 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]]

After executing the program, the output will be:

Enter the number of elements in list: 5 Enter element1: test Enter element2: test Enter element3: my Enter element4: world Enter element5: world ['test', 'test', 'my', 'world', 'world'] Enter word to remove: world Enter the occurrence to remove: 4 The number of repetitions is: 2 Updated list is: ['test', 'test', 'my', 'world', 'world'] The distinct elements are: {'test', 'world', 'my'}

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

There are numerous methods for reading a list of words and removing the ith occurrence of the given word in a list where words can repeat themselves.

  • Reading a list of words separated by new line and using count [User Input]
  • Reading a list of words separated by space and using count [User Input]

Explore more instances related to python concepts fromPython Programming ExamplesGuide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: Reading a list of words separated by new line and using count [User Input]

Approach:

  • Scan the number of elements/strings and store it in a variable
  • Take a new list say anslist
  • Take a count and initialize it to 0
  • Using a for loop, accept the values into the list and put them into the list.
  • Then use an if statement to check that the term that will be removed coincides with the element and the occurrence number.
  • If it matches then increment the count
  • If the count doesn’t matches with the occurrence value then append it to anslist.
  • Print the anslist.

Below is the implementation:

# Taking a empty list listWords = [] # Taking an empty list which gives the result say anslist anslist = [] # taking a word count and initalize it to 0 wordCount = 0 # scanning the number of strings numb = int[input["enter the total number of strings :"]] # Using for loop for i in range[numb]: elem = input["enter some random string or word :\n"] listWords.append[elem] # given word given_word = input["Enter the word which you want to remove the ith occurrence"] # given occurrence number occur = int[ input["Enter which occurrence you wish to delete of the provided term"]] # printing the list before removing the ith occurrence of word print["printing the list before removing the ith occurrence of word :"] print[listWords] # Traversing the given listWords for word in listWords: # checking if the given word mathches with the word if[word == given_word]: # increasing the count wordCount = wordCount+1 # If the count doesn't matches with the occurrence value then append it to anslist. if[wordCount != occur]: anslist.append[word] else: anslist.append[word] # printing the list before after the ith occurrence of word print["printing the list after removing the ith occurrence of word :"] print[anslist]

Output:

enter the total number of strings :8 enter some random string or word : hello enter some random string or word : this enter some random string or word : is enter some random string or word : btechgeeks enter some random string or word : this enter some random string or word : online enter some random string or word : this enter some random string or word : platform Enter the word which you want to remove the ith occurrence = this Enter which occurrence you wish to delete of the provided term = 2 printing the list before removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeeks', 'this', 'online', 'this', 'platform'] printing the list after removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeeks', 'online', 'this', 'platform']

Method #2: Reading a list of words separated by space and using count [User Input]

Approach:

  • Scan the list of words separated by space .
  • Store the above string in list using split[] function.
  • Take a new list say anslist
  • Take a count and initialize it to 0
  • Using a for loop, accept the values into the list and put them into the list.
  • Then use an if statement to check that the term that will be removed coincides with the element and the occurrence number.
  • If it matches then increment the count
  • If the count doesn’t matches with the occurrence value then append it to anslist.
  • Print the anslist.

Below is the implementation:

# Taking an empty list which gives the result say anslist anslist = [] # taking a word count and initalize it to 0 wordCount = 0 # Taking a list of words in list using split listWords = list[ input["Enter the list of words / enter the sentence\n"].split[]] # given word given_word = input[ "Enter the word which yoou want to remove the ith occurrence"] # given occurrence number occur = int[ input["Enter which occurrence you wish to delete of the provided term"]] # printing the list before removing the ith occurrence of word print["printing the list before removing the ith occurrence of word :"] print[listWords] # Traversing the given listWords for word in listWords: # checking if the given word mathches with the word if[word == given_word]: # increasing the count wordCount = wordCount+1 # If the count doesn't matches with the occurrence value then append it to anslist. if[wordCount != occur]: anslist.append[word] else: anslist.append[word] # printing the list before after the ith occurrence of word print["printing the list after removing the ith occurrence of word :"] print[anslist]

Output:

Enter the list of words / enter the sentence hello this is btechgeeks online this platform this Enter the word which yoou want to remove the ith occurrence = this Enter which occurrence you wish to delete of the provided term =2 printing the list before removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeejsks', 'this', 'is', 'online', 'this', 'platform', 'this'] printing the list after removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeejsks', 'is', 'online', 'this', 'platform', 'this']

Related Programs:

  • Python Program to Remove the Characters of Odd Index Values in a String
  • Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions
  • Python Program to Take in a String and Replace Every Blank Space with Hyphen
  • Python Program to Take in the Marks of 5 Subjects and Display the Grade

Related

“write a python program to remove the ith occurrence of the given word in a list where words can repeated.” Code Answer


remove all occurrences of a character in a list python
python by Shaunak on Jun 04 2020 Comment
3
Source: stackoverflow.com
Add a Grepper Answer

Python answers related to “write a python program to remove the ith occurrence of the given word in a list where words can repeated.”

  • python remove duplicates words from string
  • remove specific word from string using python
  • numpy reg ex delete words before a specific character
  • Python program to remove duplicate characters of a given string.
  • python remove multiple characters from string
  • drop all characters after a character in python
  • remove occurence of character from string python
  • how to remove every other letter from a string python
  • pythonremove all instances from a list
  • count repeated characters in a string python
  • how to find most repeated word in a string in python
  • How to remove all characters after a specific character in python?
  • remove all occurences
  • python remove second occurrence of character in string
  • repetition of word in python
  • Python Remove all occurrences of a character from a string

Python queries related to “write a python program to remove the ith occurrence of the given word in a list where words can repeated.”

  • python remove all occurrences from list
  • how to remove all occurrences of a character from a list in python
  • remove all occurrences of element from list python
  • remove all occurrences of a character in a string python
  • how to remove all matching elements in a python list
  • how to remove all occurrences of an element from a list in python
  • how to remove all occurances of a list in python
  • remove all of same value python list
  • remove all occurrences in list python
  • remove all occurrences of a character in string python
  • remove all occurrences of an item in a list python
  • python remove all matching items from list
  • how to remove all instances of an element from a list python
  • remove all instances of something from list python
  • how to remove all occurrences of a character from a string in python
  • python remove all from list
  • python remove occurrences from list
  • python how to take out every instance of a number in a list
  • remove all occurrences in a list python
  • remove all same elements from list python
  • python removeall
  • python remove all occurrences of element from list
  • remove all occurrences of an element from array in python
  • remove all occurrence from list python
  • remove all elements from list python by value
  • python remove occurrences from string
  • remove all occurrences of an element from array python
  • string remove all occurrences of character python
  • remove all occurrences of character from string python
  • remove x from all elements in list python
  • find all occurrences of a character in a string python
  • remove occurrences from list python
  • write a python program to remove the ith occurrence of the given word in a list where words can repeated.
  • how to remove all occurrences of an element in python list
  • how to remove all specifed values from list
  • how to remove all occurrences of an item from a list in python
  • remove all occurrences of a element in a list python
  • python remove all elements with value from list
  • remove all instances of item from list python
  • how to delete all occurrences of element in list python with pop
  • remove all '' from list python
  • how to remove all occurences of an element from a list in python and return the list
  • remove all occurrences of an element in a list
  • does .remove remove all the occurances in python list
  • delete all the occurrences of a element from a list
  • how to remove all the occurrences of an element from a list in python
  • remove forst occurence element list python
  • remove all occurences of item ending with 5 in python
  • remove all occurences of a specific number in python
  • remove all occurences of value in list oython
  • remove all occurance of character list python
  • remove all item occurances python
  • how to remove all occurences of a number from a list python
  • remove all occurence from list
  • remove all occurrences of a character in a stringpython
  • remove all occurences of a list python
  • how to remove every occurrences of a variable in python
  • delete all occurences in list python
  • remove all occurrences of a number in a list python
  • remove all occurances from list python
  • remove all occurrences of a element from a string python
  • how to remove all occurences of a melement in a list
  • remove all occurrences of character in string python
  • remove all instances of a particular word from a list in python
  • remove all occurences of item in list
  • python list remove all specific character
  • remove all occurrences of an element from list python
  • python remove item with specified number of occurences
  • does remove in python remove all occurences
  • remove all occurance from list in python
  • remove all occurences of a word from a list in python
  • remove all occurence of a character in a string using python
  • removing all occurance of a charecter from a string in python
  • remove all occurrence of character in list python
  • removing all occurences of a number from a list python
  • remove all occurrences of an element in a string python
  • python remove all occurrences from list in list
  • remove all occurrences of word from a list in pythob
  • remove all the occurrences of an element from a list in java
  • remove all occurances of element in list python
  • remove all occurrences of an element in a list python
  • removing all occurrences of item in list
  • removeall elements list with value python
  • hwo ot remove all instance of value in list python
  • how to remove all values in a list that are equal to a certain value
  • removeall python list
  • python remove all list
  • python remove all form list
  • remove occurrence in list python
  • remove all instance in list
  • python remove all elements from and array that match a value
  • python remove all 0 from list
  • remove all instances of a value in a list python
  • remove every occurrence list python
  • python array removeall
  • how to remove all the object from a list
  • remove all instances python list
  • remove all list elements with certain value python
  • how to pop all occurences of a value in a list python
  • python remove all given elements in list
  • remove all 2 in python list
  • erase all instances in list python
  • removing all instance of particular value from list in python
  • remove all entries from array that has value python
  • take off all occurences of \n python
  • python remove all elements from list by value
  • remove all occurrences of string in list python
  • remove all occurrences in string python
  • remove all occurrences of an element in a list python inp`lace
  • remove all occurrences from list
  • array remove python all instances
  • python remove all occurrences of character from string
  • python remove all occurrences of value from list
  • remove every occurence of a character in python
  • code to remove all the occurrences of a number from a list
  • how to remove all occurrences of an element from a string in python
  • how to remove all elements that are 0 in a list python
  • python remove all 0s from list
  • how to delete all occurrences of a value in python list
  • does list.remove remove all instances poython
  • remove every all 5's from a list of numbers in python
  • how to remove all a in a list
  • does python.remove remove all instances
  • does .remove remove all items or just the first one python
  • python remove all ''
  • remove all occurrences of an element in a linked list
  • remove all from python list
  • delete all 0 in list python
  • remove all occurrences of an element in a list python
  • first, it removes all elements from the list that have greater values than val
  • python list remove all occurrences
  • delete all occurrences of an element from a list python
  • delete all occurrences in list python
  • python remove all occurrences from string
  • remove all instances of an element from list python
  • remove occurrence from list python
  • how to remove all instances of a character from a list python
  • how to remove all zeros from a list in python
  • remove all values for the list that equal to
  • how to remove all instances of a specific character from a list in python
  • remove all occurrences from string python
  • list remove all python
  • remove all matching elements from list python
  • removeall in python
  • remove all values from list python
  • remove all occurrences from list
  • remove all from list python
  • delete occurence python
  • remove all instances in list python
  • python remove all instances of value from list
  • remove all elements from list python
  • remove all occurrences of a substring in a string python
  • delete all zeros from list python
  • remove all occurrences of an items in a list python
  • how to pop out all the 0 of a list in python
  • python how to remove all occurrences of a character from a list
  • remove all occurrences of an element in a list using loop in python
  • removing all occurrences of an element from list
  • the remove method removes all occurrences of an item from a list.
  • remove all occurrences of a string in a list python
  • remove all occurrences of a specific value from a list python
  • remove numbers at all occurrences in list python
  • python string remove all occurrences of character
  • remove all instances of a character from a list in python
  • remove all zeros from list python
  • how to remove all values from a list in python
  • python list remove all instances of value
  • remove all occurrences a given element from the list python
  • remove all occurences of a value from and array python
  • delete same instances in lis python
  • how to delete all occurences list element in python
  • how to remove all occurances of an item in a list
  • remove all occurrences of specsifc element from list python
  • python remove all occurence in a list
  • remove all occurence py list
  • remove all occurrences of an element from list in python
  • remove all occurences in python
  • remove all occurences of value in list python
  • remove all occurances python
  • delete all occurrences in list
  • remove all occurances of py string
  • how to remove all occurances of a word in a list python
  • remove all occurrences of a number in an array python
  • python remove all occuracent of element in list
  • how to delete all occurance of element in list
  • delete all occurrences of character from the string python
  • python remove all occurrences of character
  • how to remove all occurences of a value in python
  • delete all occurence of value from list in python
  • .remove in python list removes all occurences?
  • does remove method removes all occurrences of an item from a list.
  • python remove all occurances of a list
  • how to remove a specfic character from an entire list in python
  • delete all occurances in a list
  • remove all occurence of values of a lis
  • python remove n occurrences from list
  • delete all occurences of a number in a list
  • delete all the occurrences of an element from a list
  • list delete occurrences python
  • how to delete all occurrences of element in list python with del
  • python delete all occurrences from string
  • python remoce all occurences from list
  • how to remove item all occurances from python list
  • remove all occurrences of specified element in list python
  • remove all occurrences of specific element in list python
  • hwo to remove all occurences in python
  • remove all occurrences of an element in a list prolo
  • how to remove all occurence of a letter in a list in python
  • remove all the instances of a number from list python
  • remove all in python
  • delete all equal elements from an array python
  • how to remove all same items from a list in python
  • python remove reocurrences from list
  • how to delete all occurances of an item from list in python
  • remove all occurences of a value from a numpy array
  • removing all instances in python list
  • remove all value from list python
  • python list remove all items equal ro
  • remove all elements that equal in python
  • python remove all cases
  • remove all items from list where value is equal to python
  • remove all 0 from list python
  • remove all occurances of value in list
  • delete all elements with certain value python
  • remove all occurance elements from list python
  • python remove all elements with value from array
  • delete all entries with some value in list python
  • python remove all given elements
  • removing all occurrences of an element from list python
  • how to delete all occurrences of a element in a list
  • how to remove all element from list python by value
  • how to remove all instances of a word from a list in python
  • how to remove all occurrences element from list in python
  • python list remove all occurences
  • remove all occurrences of a word in a list python
  • remove all element of occurrences in python
  • how to remove all occurences of an element in the list
  • remove all occurences from list python
  • delete all occurrences of an element from a list in place python
  • python remove all occurrences of character in string
  • python re remove all occurrences
  • remove all occurrences an element from python list
  • remove occurences of number in list
  • how to remove an index from a list in python
  • how to remove all 1s from a list pyton
  • python remove all occurrences of string in string
  • python remove matching items from list
  • remove all x in list
  • remove items eqal to in list python
  • the remove method in python remove all occurrences or what ?
  • remove multiple occurences in list using .remove
  • remove all occurance of number in a list from another list python
  • remove all elements equal to python
  • python list removeall occurrences of element
  • remove all items from a list with a given value
  • remove all occurences of list in python
  • remove all occurrences of a character in a list python
  • remove all occurrences from list python
  • remove all occurrences of element in list python
  • list remove all occurrences python
  • how to remove all occurrences of element in list python
  • delete all values that match python
  • remove all instances from list python
  • python remove all
  • how to remove all occurrences of a substring from a string in python
  • remove all the occurrences of an element from a list in python
  • python remove all occurences from list
  • python delete all occurrences from list
  • how to remove all occurrences of a number from a list in python
  • how to remove all 0 from list python
  • remove all occurances of an element from list python
  • remove all instances of element from list python
  • remove all occurrences of item in list python
  • remove all python
  • python list remove all
  • remove all occurance of element in list python
  • remove zero from list python
  • remove all occurrences of a word in a string python
  • remove all occurences in list python
  • python remove all elements matching paramater
  • python array remove all occurrences
  • how to remove every instance of item in list python
  • python remove all x form list
  • remove all occurrences from a string python
  • how to remove all occurrences of an element in a list
  • pytho remove matching values from a list
  • remove all occurrences of a value in a list python
  • how to remove all same items in a list
  • python list remove all occurrences of an element
  • remove all value list contain python
  • how to remove all occurrences of an element from list in python
  • how to remove all same elements from list in python
  • python all any specific item list
  • python remove all occurrences from array
  • how to remove all instances of a string in a list in python
  • remove all of a value from list python
  • remove all occurrences of an element from list python
  • remove all occurrences of specific element from list python
  • remove all occurrences of a str in a list
  • python remove all occurrence of an items from list
  • array remove all occurrences python
  • how to delete all the occurences of an entry in python list
  • delete all occurrences of an element in a list python
  • how to delete all occurrences of a specific element in list
  • python remove all occurrences of item from list
  • remove all occurences function in python
  • delete all occurrences of an element in list python
  • remove all occure of an item from list python
  • remove one occurrence from list python
  • remove all occurrences from a list python
  • how to remove all occurrences of a character from a list in python
  • remove all occurence of a number in an array python
  • remove all occurance of number from list python
  • how to remove all occurunces of a value in a list python
  • the remove method removes all occurrences of an item from a list
  • delete all string occurrences in list python
  • delete all occurrences of value from list in python inplace
  • python list how to remove all occurnce
  • remove every occurrence of the number x from this list python
  • remove all occurences from python list
  • remove all occurrences of a particular element from a string python
  • remove all occurrences from list pythob
  • remove all occurence of values of a list
  • python remove all occurrences from list
  • remove all occurences of element in list
  • to remove all occurences of an element in python list
  • remove all words from list that doesnt contain my characters python
  • remove all occurrences of an element in a list java
  • how to remove all occurrences of element in list python with del
  • remove all occurences of objkectin string in python
  • how to remove all occurrence of element from list in python
  • remove[ ] function removes the ________ occurrences of an element from the list
  • python remove all occurances from list
  • python remove all occurrecnes from list
  • how to remove all occurence of a character in a list in python
  • how to remove all occurence of a string in a list in python
  • remove all element of a value from a list python
  • how to remove all instances of an value in a list python
  • remove all in list python
  • drop all occurrences list
  • remove all matching items from list python
  • python list remove all matches
  • how to remove all occurance of object in list
  • removing all instances in python
  • remove all occurrences of a elementin a list python
  • remove all certain elements from list python
  • remove matching element from list python
  • remove all occurrences of a item in a list python
  • how to remove all inctsances of a element in a list python
  • python remove elements euqlas between lists
  • delete all elemtent siwth value python
  • python3 list remove all elements
  • delete all occrances of value from python list
  • list.remove all python
  • remove all python array
  • remove multiple occurences element from a list in python
  • delete all occurrences of element from the list python
  • pop all same value elements in python
  • delete all occurrences of character from the list python
  • remove all x from list python
  • remove occurrences in a list python
  • delete all occurrences of an element from a list
  • remove all occurrences of word python
  • remove all occurences in list
  • remove all word occuring in list
  • how to remove all occurrences before a cetain character in "python"
  • remove all occurrences of a character in a string from end python
  • remove all occurances of element list python
  • how to delete all occurrences of a word in a sentence python
  • remove all occurrences an element from an array python
  • remove all occurrences of a single character from a string python
  • how to remove all from a list
  • python remove all instance from list
  • python remove element from list by value all occurrences a given
  • list.removeall python
  • remove all 5's from a list of numbers in python
  • how to remove all of one element in a list
  • python remove all characters from string except characters in list
  • remove 0 from list python
  • remove all occrance of an elements python
  • remove all in a list python
  • python list removeall occurence of element
  • remove all elemnts with same value list

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

PythonServer Side ProgrammingProgramming

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 −

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

There are numerous methods for reading a list of words and removing the ith occurrence of the given word in a list where words can repeat themselves.

  • Reading a list of words separated by new line and using count [User Input]
  • Reading a list of words separated by space and using count [User Input]

Explore more instances related to python concepts fromPython Programming ExamplesGuide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: Reading a list of words separated by new line and using count [User Input]

Approach:

  • Scan the number of elements/strings and store it in a variable
  • Take a new list say anslist
  • Take a count and initialize it to 0
  • Using a for loop, accept the values into the list and put them into the list.
  • Then use an if statement to check that the term that will be removed coincides with the element and the occurrence number.
  • If it matches then increment the count
  • If the count doesn’t matches with the occurrence value then append it to anslist.
  • Print the anslist.

Below is the implementation:

# Taking a empty list listWords = [] # Taking an empty list which gives the result say anslist anslist = [] # taking a word count and initalize it to 0 wordCount = 0 # scanning the number of strings numb = int[input["enter the total number of strings :"]] # Using for loop for i in range[numb]: elem = input["enter some random string or word :\n"] listWords.append[elem] # given word given_word = input["Enter the word which you want to remove the ith occurrence"] # given occurrence number occur = int[ input["Enter which occurrence you wish to delete of the provided term"]] # printing the list before removing the ith occurrence of word print["printing the list before removing the ith occurrence of word :"] print[listWords] # Traversing the given listWords for word in listWords: # checking if the given word mathches with the word if[word == given_word]: # increasing the count wordCount = wordCount+1 # If the count doesn't matches with the occurrence value then append it to anslist. if[wordCount != occur]: anslist.append[word] else: anslist.append[word] # printing the list before after the ith occurrence of word print["printing the list after removing the ith occurrence of word :"] print[anslist]

Output:

enter the total number of strings :8 enter some random string or word : hello enter some random string or word : this enter some random string or word : is enter some random string or word : btechgeeks enter some random string or word : this enter some random string or word : online enter some random string or word : this enter some random string or word : platform Enter the word which you want to remove the ith occurrence = this Enter which occurrence you wish to delete of the provided term = 2 printing the list before removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeeks', 'this', 'online', 'this', 'platform'] printing the list after removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeeks', 'online', 'this', 'platform']

Method #2: Reading a list of words separated by space and using count [User Input]

Approach:

  • Scan the list of words separated by space .
  • Store the above string in list using split[] function.
  • Take a new list say anslist
  • Take a count and initialize it to 0
  • Using a for loop, accept the values into the list and put them into the list.
  • Then use an if statement to check that the term that will be removed coincides with the element and the occurrence number.
  • If it matches then increment the count
  • If the count doesn’t matches with the occurrence value then append it to anslist.
  • Print the anslist.

Below is the implementation:

# Taking an empty list which gives the result say anslist anslist = [] # taking a word count and initalize it to 0 wordCount = 0 # Taking a list of words in list using split listWords = list[ input["Enter the list of words / enter the sentence\n"].split[]] # given word given_word = input[ "Enter the word which yoou want to remove the ith occurrence"] # given occurrence number occur = int[ input["Enter which occurrence you wish to delete of the provided term"]] # printing the list before removing the ith occurrence of word print["printing the list before removing the ith occurrence of word :"] print[listWords] # Traversing the given listWords for word in listWords: # checking if the given word mathches with the word if[word == given_word]: # increasing the count wordCount = wordCount+1 # If the count doesn't matches with the occurrence value then append it to anslist. if[wordCount != occur]: anslist.append[word] else: anslist.append[word] # printing the list before after the ith occurrence of word print["printing the list after removing the ith occurrence of word :"] print[anslist]

Output:

Enter the list of words / enter the sentence hello this is btechgeeks online this platform this Enter the word which yoou want to remove the ith occurrence = this Enter which occurrence you wish to delete of the provided term =2 printing the list before removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeejsks', 'this', 'is', 'online', 'this', 'platform', 'this'] printing the list after removing the ith occurrence of word : ['hello', 'this', 'is', 'btechgeejsks', 'is', 'online', 'this', 'platform', 'this']

Related Programs:

  • Python Program to Remove the Characters of Odd Index Values in a String
  • Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions
  • Python Program to Take in a String and Replace Every Blank Space with Hyphen
  • Python Program to Take in the Marks of 5 Subjects and Display the Grade

Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề