w3resource

Python: Remove specific words from a given list

Python List: Exercise - 148 with Solution

Write a Python program to remove specific words from a given list.

Sample Solution:

Python Code:

def remove_words(list1, remove_words):
    for word in list(list1):
        if word in remove_words:
            list1.remove(word)
    return list1        
colors = ['red', 'green', 'blue', 'white', 'black', 'orange']
remove_colors = ['white', 'orange']
print("Original list:")
print(colors)
print("\nRemove words:")
print(remove_colors)
print("\nAfter removing the specified words from the said list:")
print(remove_words(colors, remove_colors))

Sample Output:

Original list:
['red', 'green', 'blue', 'white', 'black', 'orange']

Remove words:
['white', 'orange']

After removing the specified words from the said list:
['red', 'green', 'blue', 'black']

Flowchart:

Flowchart: Remove specific words from a given list.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to interleave two given list into another list randomly.
Next: Write a Python program to get all possible combinations of the elements of a given list.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Big Number Readability:

you can separate zeros with underscore (_)

print(2_000_000)
print(2_000_000 +1)

Output:

2000000
2000001