{"id":8493,"date":"2021-09-30T16:00:17","date_gmt":"2021-09-30T10:30:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=8493"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-remove-the-ith-occurrence-of-the-given-word-in-a-list-where-words-can-repeat","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-the-ith-occurrence-of-the-given-word-in-a-list-where-words-can-repeat\/","title":{"rendered":"Python Program to Remove the ith Occurrence of the Given Word in a List where Words can Repeat"},"content":{"rendered":"
Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n Lists in Python:<\/strong><\/p>\n 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.<\/p>\n 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?<\/p>\n 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.<\/p>\n Given the list of words the task is to remove the ith Occurrence of the word in the given list of words in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews. 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 …<\/p>\ngiven list of words =['hello', 'this', 'is', 'btechgeeks', 'this', 'online', 'this', 'platform'] , occurrence=2 , word =this<\/pre>\n
Enter the word which you want to remove the ith occurrence = this\r\nEnter which occurrence you wish to delete of the provided term = 2\r\nprinting the list before removing the ith occurrence of word :\r\n['hello', 'this', 'is', 'btechgeeks', 'this', 'online', 'this', 'platform']\r\nprinting the list after removing the ith occurrence of word :\r\n['hello', 'this', 'is', 'btechgeeks', 'online', 'this', 'platform']<\/pre>\n
given list of words =['this', 'is', 'is', 'is', 'this'] , occurrence=2 , word =is<\/pre>\n
Enter the word which yoou want to remove the ith occurrence = is\r\nEnter which occurrence you wish to delete of the provided term = 2\r\nprinting the list before removing the ith occurrence of word :\r\n['this', 'is', 'is', 'is', 'this']\r\nprinting the list after removing the ith occurrence of word :\r\n['this', 'is', 'is', 'this']<\/pre>\n
Python Program to Remove the ith Occurrence of the Given Word in a List where Words can Repeat<\/h2>\n
\n
Method #1: Reading a list of words separated by new line and using count (User Input)<\/h3>\n
\n
# Taking a empty list\r\nlistWords = []\r\n# Taking an empty list which gives the result say anslist\r\nanslist = []\r\n# taking a word count and initalize it to 0\r\nwordCount = 0\r\n# scanning the number of strings\r\nnumb = int(input(\"enter the total number of strings :\"))\r\n# Using for loop\r\nfor i in range(numb):\r\n elem = input(\"enter some random string or word :\\n\")\r\n listWords.append(elem)\r\n# given word\r\ngiven_word = input(\"Enter the word which you want to remove the ith occurrence\")\r\n# given occurrence number\r\noccur = int(\r\n input(\"Enter which occurrence you wish to delete of the provided term\"))\r\n\r\n# printing the list before removing the ith occurrence of word\r\nprint(\"printing the list before removing the ith occurrence of word :\")\r\nprint(listWords)\r\n# Traversing the given listWords\r\nfor word in listWords:\r\n # checking if the given word mathches with the word\r\n if(word == given_word):\r\n # increasing the count\r\n wordCount = wordCount+1\r\n # If the count doesn't matches with the occurrence value then append it to anslist.\r\n if(wordCount != occur):\r\n anslist.append(word)\r\n else:\r\n anslist.append(word)\r\n# printing the list before after the ith occurrence of word\r\nprint(\"printing the list after removing the ith occurrence of word :\")\r\nprint(anslist)\r\n<\/pre>\n
enter the total number of strings :8\r\nenter some random string or word :\r\nhello\r\nenter some random string or word :\r\nthis\r\nenter some random string or word :\r\nis\r\nenter some random string or word :\r\nbtechgeeks\r\nenter some random string or word :\r\nthis\r\nenter some random string or word :\r\nonline\r\nenter some random string or word :\r\nthis\r\nenter some random string or word :\r\nplatform\r\nEnter the word which you want to remove the ith occurrence = this\r\nEnter which occurrence you wish to delete of the provided term = 2\r\nprinting the list before removing the ith occurrence of word :\r\n['hello', 'this', 'is', 'btechgeeks', 'this', 'online', 'this', 'platform']\r\nprinting the list after removing the ith occurrence of word :\r\n['hello', 'this', 'is', 'btechgeeks', 'online', 'this', 'platform']\r\n\r\n<\/pre>\n
Method #2: Reading a list of words separated by space and using count (User Input)<\/h3>\n
\n
# Taking an empty list which gives the result say anslist\r\nanslist = []\r\n# taking a word count and initalize it to 0\r\nwordCount = 0\r\n# Taking a list of words in list using split\r\nlistWords = list(\r\n input(\"Enter the list of words \/ enter the sentence\\n\").split())\r\n\r\n# given word\r\ngiven_word = input(\r\n \"Enter the word which yoou want to remove the ith occurrence\")\r\n# given occurrence number\r\noccur = int(\r\n input(\"Enter which occurrence you wish to delete of the provided term\"))\r\n\r\n# printing the list before removing the ith occurrence of word\r\nprint(\"printing the list before removing the ith occurrence of word :\")\r\nprint(listWords)\r\n# Traversing the given listWords\r\nfor word in listWords:\r\n # checking if the given word mathches with the word\r\n if(word == given_word):\r\n # increasing the count\r\n wordCount = wordCount+1\r\n # If the count doesn't matches with the occurrence value then append it to anslist.\r\n if(wordCount != occur):\r\n anslist.append(word)\r\n else:\r\n anslist.append(word)\r\n# printing the list before after the ith occurrence of word\r\nprint(\"printing the list after removing the ith occurrence of word :\")\r\nprint(anslist)\r\n<\/pre>\n
Enter the list of words \/ enter the sentence\r\nhello this is btechgeeks online this platform this\r\nEnter the word which yoou want to remove the ith occurrence = this\r\nEnter which occurrence you wish to delete of the provided term =2\r\nprinting the list before removing the ith occurrence of word :\r\n['hello', 'this', 'is', 'btechgeejsks', 'this', 'is', 'online', 'this', 'platform', 'this']\r\nprinting the list after removing the ith occurrence of word :\r\n['hello', 'this', 'is', 'btechgeejsks', 'is', 'online', 'this', 'platform', 'this']<\/pre>\n
\n