Bird
0
0

Which Python code snippet correctly determines if any banned word from the list banned_words appears in the variable output?

easy📝 Syntax Q3 of 15
Agentic AI - Agent Safety and Guardrails
Which Python code snippet correctly determines if any banned word from the list banned_words appears in the variable output?
Afiltered = any(word in output for word in banned_words)
Bfiltered = output in banned_words
Cfiltered = banned_words in output
Dfiltered = output.contains(banned_words)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to check if any word from banned_words is present in output.
  2. Step 2: Analyze each option

    • filtered = any(word in output for word in banned_words) uses any() with a generator expression checking membership of each word in output, which is correct.
    • filtered = output in banned_words incorrectly checks if output is in the list banned_words, which is the reverse.
    • filtered = banned_words in output checks if the entire list banned_words is in output, which is invalid.
    • filtered = output.contains(banned_words) uses a non-existent method contains on a string.
  3. Final Answer:

    filtered = any(word in output for word in banned_words) -> Option A
  4. Quick Check:

    Check if any banned word is substring of output [OK]
Quick Trick: Use any() with a generator to check membership [OK]
Common Mistakes:
  • Checking if output is in banned_words instead of the reverse
  • Using invalid methods like contains() on strings
  • Trying to check if the entire list is in the output string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes