{"id":12851,"date":"2021-09-30T12:30:41","date_gmt":"2021-09-30T07:00:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=12851"},"modified":"2021-11-22T18:34:38","modified_gmt":"2021-11-22T13:04:38","slug":"python-program-to-remove-a-string-from-a-list-of-strings","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-a-string-from-a-list-of-strings\/","title":{"rendered":"Python Program to Remove a String from a List of Strings"},"content":{"rendered":"
Given a list of strings and another string the task is to remove the given string from the given list of strings.<\/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 Below are the ways to remove the given string from the given list of strings.<\/p>\n Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/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 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 Given a list of strings and another string the task is to remove the given string from the given list of strings. Examples: Example1: Input: Given list of strings =[‘hello’, ‘this’, ‘is’, ‘btechgeeks’, ‘python’, ‘coding’, ‘platform’] Given string =’python’ Output: The given list of strings before removing the string [ python ] is [‘hello’, ‘this’, …<\/p>\nGiven list of strings =['hello', 'this', 'is', 'btechgeeks', 'python', 'coding', 'platform']\r\nGiven string ='python'<\/pre>\n
The given list of strings before removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding',\r\n 'platform']\r\nThe given list of strings after removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'coding', 'platform']<\/pre>\n
Given list of strings =['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform', \r\n 'for', 'geeks', 'online']\r\nGiven string ='geeks'<\/pre>\n
The given list of strings before removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', \r\n 'online', 'coding', 'platform', 'for', 'geeks', 'online']\r\nThe given list of strings after removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', 'btechgeeks',\r\n 'online', 'coding', 'platform', 'for', 'online']<\/pre>\n
Program to Remove a String from a List of Strings in Python<\/h2>\n
\n
Method #1: Using List Comprehension (Static Input)<\/h3>\n
\n
# Give the list of strings as static input and store it in a variable.\r\ngvnstrnlists = ['hello', 'this', 'is',\r\n 'btechgeeks', 'python', 'coding', 'platform']\r\n# Give the string as static input and store it in another variable.\r\ngvnstrng = \"python\"\r\nprint(\r\n 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists)\r\n\r\n# Remove the given strings from the list of strings using List Comprehension.\r\nmodilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng]\r\n# Print the modified list of strings.\r\nprint(\r\n 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)\r\n<\/pre>\n
The given list of strings before removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding',\r\n 'platform']\r\nThe given list of strings after removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'coding', 'platform']<\/pre>\n
Method #2: Using List Comprehension (User Input)<\/h3>\n
\n
# Give the list of strings as user input using list(), split(),\r\n# and input() functions and store it in a variable.\r\ngvnstrnlists = list(input('Enter some random list of strings = ').split())\r\n# Give the string as user input using input() and store it in another variable.\r\ngvnstrng = input('enter some random string which you want to remove = ')\r\nprint(\r\n 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists)\r\n\r\n# Remove the given strings from the list of strings using List Comprehension.\r\nmodilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng]\r\n# Print the modified list of strings.\r\nprint(\r\n 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)\r\n<\/pre>\n
Enter some random list of strings = good morning this is btechgeeks python\r\nenter some random string which you want to remove = this\r\nThe given list of strings before removing the string [ this ] is ['good', 'morning', 'this', 'is', 'btechgeeks', 'python']\r\nThe given list of strings after removing the string [ this ] is ['good', 'morning', 'is', 'btechgeeks', 'python']<\/pre>\n
Method #3: Using remove() function (Static Input)<\/h3>\n
\n
# Give the list of strings as static input and store it in a variable.\r\ngvnstrnlists = ['hello', 'this', 'is',\r\n 'btechgeeks', 'python', 'coding', 'platform']\r\n# Give the string as static input and store it in another variable.\r\ngvnstrng = \"python\"\r\nprint(\r\n 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists)\r\n# Loop through the list of strings using For loop.\r\nfor strngele in gvnstrnlists:\r\n # Check if the element is equal to the given string using the If statement.\r\n if(strngele == gvnstrng):\r\n # If it is true then remove the element from the given list\r\n # of strings using the remove() function.\r\n gvnstrnlists.remove(strngele)\r\n# Remove the given strings from the list of strings using List Comprehension.\r\nmodilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng]\r\n# Print the modified list of strings.\r\nprint(\r\n 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)\r\n<\/pre>\n
The given list of strings before removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding', 'platform']\r\nThe given list of strings after removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'coding', 'platform']<\/pre>\n
Method #4: Using remove() function (User Input)<\/h3>\n
\n
# Give the list of strings as user input using list(), split(),\r\n# and input() functions and store it in a variable.\r\ngvnstrnlists = list(input('Enter some random list of strings = ').split())\r\n# Give the string as user input using input() and store it in another variable.\r\ngvnstrng = input('enter some random string which you want to remove = ')\r\nprint(\r\n 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists)\r\n# Loop through the list of strings using For loop.\r\nfor strngele in gvnstrnlists:\r\n # Check if the element is equal to the given string using the If statement.\r\n if(strngele == gvnstrng):\r\n # If it is true then remove the element from the given list\r\n # of strings using the remove() function.\r\n gvnstrnlists.remove(strngele)\r\n# Remove the given strings from the list of strings using List Comprehension.\r\nmodilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng]\r\n# Print the modified list of strings.\r\nprint(\r\n 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)\r\n<\/pre>\n
Enter some random list of strings = hello food morning this is btechgeeks online coding platform\r\n for geeks online\r\nenter some random string which you want to remove = geeks\r\nThe given list of strings before removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', \r\n'btechgeeks', 'online', 'coding', 'platform', 'for', 'geeks', 'online']\r\nThe given list of strings after removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is',\r\n 'btechgeeks', 'online', 'coding', 'platform', 'for', 'online']<\/pre>\n
\n