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:
    return list1        
colors = ['red', 'green', 'blue', 'white', 'black', 'orange']
remove_colors = ['white', 'orange']
print("Original list:")
print("\nRemove words:")
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: 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 Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Given a predicate function, fn, and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate:


def tips_check_prop(fn, prop):
  return lambda obj: fn(obj[prop])
check_age = tips_check_prop(lambda x: x >= 25, 'age')
user = {'name': 'Owen', 'age': 25}





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook