We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Filter Messages

You're about to write a bit more code in a single function than you have before.

Do not try to write it all at once. Start with the outermost loop and work your way inward. Add extra print() statements and run your code often to make sure it's doing what you expect. Just make sure to remove the extra print() statements before submitting your code.

Running your code often to make sure each line is doing what you expect is called "debugging." All programmers, even seasoned professionals, break large problems down into small ones that they can debug line by line.

Because we're working with strings that need to be converted to lists and vice versa, here are a couple helpful Python methods we can use to make our lives much easier.

Split a String Into a List of Words

The .split() method in Python is called on a string and returns a list by splitting the string based on a given delimiter. If no delimiter is provided, it will split the string on whitespace. Here's a quick example:

message = "hello there sam"
words = message.split()
print(words)
# Prints: ["hello", "there", "sam"]

Join a List of Strings Into a Single String

The .join() method is called on a delimiter (what goes between all the words in the list), and takes a list of strings as input.

list_of_words = ["hello", "there", "sam"]
sentence = " ".join(list_of_words)
print(sentence)
# Prints: "hello there sam"

Assignment

We need to filter the profanity out of our game's live chat feature! Complete the filter_messages function. It takes a list of chat messages as input and returns two new lists:

  1. A list of the same messages but with all instances of the word dang removed.
  2. A list containing the number of dang words that were removed from each message at that particular index.

Here are some examples:

messages = ["dang it bobby!", "look at it go"]
filter_messages(messages)  # returns ["it bobby!", "look at it go"], [1, 0]

messages2 = [
    "That's the bloody dang Reaper of Mars...",
    "Pax au Telemanus!",
    "I was never taught how to use a dang razor!",
]
filter_messages(
    messages2
)  # returns ["That's the bloody Reaper of Mars...", "Pax au Telemanus!", "I was never taught how to use a razor!"], [1, 0, 1]

Here are the steps for you to follow. There are a LOT of steps! It won't be easy, but if you follow the steps exactly, you will pass this assignment: