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

Lists in Python:

We’ve learnt a lot of things so far, from printing something to making a decision to iterating with loops. This and the following chapters will be about storing data. So, let’s begin with a list, which is used to hold a collection of facts.

In our program, we frequently require a list. Assume you’re creating a software to save the grades of all 50 students in a class. Considering 50 distinct factors is not a smart idea. Instead, we can store all 50 values (marks) in a single variable as a list. Isn’t it cool?

The Python list is a straightforward ordered list of items. Python lists are extremely powerful since the objects in them do not have to be homogeneous or even of the same type. Strings, numbers, and characters, as well as other lists, can all be found in a Python list. Python lists are also mutable: once stated, they can be easily altered via a variety of list methods. Let’s look at how to use these techniques to handle Python lists.

Given the list of words the task is to remove the ith Occurrence of the word in the given list of words in Python.

Examples:

Example1:

Input:

given list of words =['hello', 'this', 'is', 'btechgeeks', 'this', 'online', 'this', 'platform'] , occurrence=2 , word =this

Output:

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']

Example2:

Input:

given list of words =['this', 'is', 'is', 'is', 'this'] , occurrence=2 , word =is

Output:

Enter the word which yoou want to remove the ith occurrence = is
Enter which occurrence you wish to delete of the provided term = 2
printing the list before removing the ith occurrence of word :
['this', 'is', 'is', 'is', 'this']
printing the list after removing the ith occurrence of word :
['this', 'is', 'is', 'this']

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.

Explore more instances related to python concepts from Python Programming Examples Guide 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']

Leave a Comment