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', 'is', 'btechgeeks', 'python', 'coding', 'platform'] The given list of strings after removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'coding', 'platform']
Example2:
Input:
Given list of strings =['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform', 'for', 'geeks', 'online'] Given string ='geeks'
Output:
The given list of strings before removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform', 'for', 'geeks', 'online'] The given list of strings after removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform', 'for', 'online']
Program to Remove a String from a List of Strings in Python
Below are the ways to remove the given string from the given list of strings.
- Using List Comprehension (Static Input)
- Using List Comprehension (User Input)
- Using remove() function (Static Input)
- Using remove() function (User Input)
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Method #1: Using List Comprehension (Static Input)
Approach:
- Give the list of strings as static input and store it in a variable.
- Give the string as static input and store it in another variable.
- Remove the given strings from the list of strings using List Comprehension.
- Print the modified list of strings.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as static input and store it in a variable. gvnstrnlists = ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding', 'platform'] # Give the string as static input and store it in another variable. gvnstrng = "python" print( 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists) # Remove the given strings from the list of strings using List Comprehension. modilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng] # Print the modified list of strings. print( 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)
Output:
The given list of strings before removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding', 'platform'] The given list of strings after removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'coding', 'platform']
Method #2: Using List Comprehension (User Input)
Approach:
- Give the list of strings as user input using list(), split(), and input() functions and store it in a variable.
- Give the string as user input using input() and store it in another variable.
- Remove the given strings from the list of strings using List Comprehension.
- Print the modified list of strings.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as user input using list(), split(), # and input() functions and store it in a variable. gvnstrnlists = list(input('Enter some random list of strings = ').split()) # Give the string as user input using input() and store it in another variable. gvnstrng = input('enter some random string which you want to remove = ') print( 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists) # Remove the given strings from the list of strings using List Comprehension. modilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng] # Print the modified list of strings. print( 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)
Output:
Enter some random list of strings = good morning this is btechgeeks python enter some random string which you want to remove = this The given list of strings before removing the string [ this ] is ['good', 'morning', 'this', 'is', 'btechgeeks', 'python'] The given list of strings after removing the string [ this ] is ['good', 'morning', 'is', 'btechgeeks', 'python']
Method #3: Using remove() function (Static Input)
Approach:
- Give the list of strings as static input and store it in a variable.
- Give the string as static input and store it in another variable.
- Loop through the list of strings using For loop.
- Check if the element is equal to the given string using the If statement.
- If it is true then remove the element from the given list of strings using the remove() function.
- Print the modified list of strings.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as static input and store it in a variable. gvnstrnlists = ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding', 'platform'] # Give the string as static input and store it in another variable. gvnstrng = "python" print( 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists) # Loop through the list of strings using For loop. for strngele in gvnstrnlists: # Check if the element is equal to the given string using the If statement. if(strngele == gvnstrng): # If it is true then remove the element from the given list # of strings using the remove() function. gvnstrnlists.remove(strngele) # Remove the given strings from the list of strings using List Comprehension. modilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng] # Print the modified list of strings. print( 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)
Output:
The given list of strings before removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'python', 'coding', 'platform'] The given list of strings after removing the string [ python ] is ['hello', 'this', 'is', 'btechgeeks', 'coding', 'platform']
Method #4: Using remove() function (User Input)
Approach:
- Give the list of strings as user input using list(), split(), and input() functions and store it in a variable.
- Give the string as user input using input() and store it in another variable.
- Loop through the list of strings using For loop.
- Check if the element is equal to the given string using the If statement.
- If it is true then remove the element from the given list of strings using the remove() function.
- Print the modified list of strings.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as user input using list(), split(), # and input() functions and store it in a variable. gvnstrnlists = list(input('Enter some random list of strings = ').split()) # Give the string as user input using input() and store it in another variable. gvnstrng = input('enter some random string which you want to remove = ') print( 'The given list of strings before removing the string [', gvnstrng, '] is', gvnstrnlists) # Loop through the list of strings using For loop. for strngele in gvnstrnlists: # Check if the element is equal to the given string using the If statement. if(strngele == gvnstrng): # If it is true then remove the element from the given list # of strings using the remove() function. gvnstrnlists.remove(strngele) # Remove the given strings from the list of strings using List Comprehension. modilststrngs = [elemen for elemen in gvnstrnlists if elemen != gvnstrng] # Print the modified list of strings. print( 'The given list of strings after removing the string [', gvnstrng, '] is', modilststrngs)
Output:
Enter some random list of strings = hello food morning this is btechgeeks online coding platform for geeks online enter some random string which you want to remove = geeks The given list of strings before removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform', 'for', 'geeks', 'online'] The given list of strings after removing the string [ geeks ] is ['hello', 'food', 'morning', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform', 'for', 'online']
Related Programs:
- python program to remove punctuations from a string
- python program to remove the ith occurrence of the given word in a list where words can repeat
- python program to form a new string made of the first 2 and last 2 characters from a given string
- python program to remove the characters of odd index values in a string
- python program to remove adjacent duplicate characters from a string
- python program to remove all consonants from a string
- python program to remove the first occurrence of character in a string