Bird
0
0

How should a code generation agent combine dictionary comprehension with a condition to create a dictionary of squares for even numbers only from 0 to 5?

hard📝 Application Q9 of 15
Agentic AI - Real-World Agent Applications

How should a code generation agent combine dictionary comprehension with a condition to create a dictionary of squares for even numbers only from 0 to 5?

A{x: x**2 for x in range(6) if x % 2 == 0}
B{x: x*2 for x in range(6) if x % 2 == 0}
C{x: x**2 for x in range(6) if x % 2 = 0}
D{x: x**2 for x in range(6) where x % 2 == 0}
Step-by-Step Solution
Solution:
  1. Step 1: Recall dictionary comprehension syntax with condition

    Use {key: value for item in iterable if condition} with '==' for comparison.
  2. Step 2: Check each option

    {x: x**2 for x in range(6) if x % 2 == 0} correctly uses '**' for square and 'if x % 2 == 0'. {x: x*2 for x in range(6) if x % 2 == 0} doubles instead of squares. {x: x**2 for x in range(6) if x % 2 = 0} uses '=' instead of '=='. {x: x**2 for x in range(6) where x % 2 == 0} uses invalid 'where' keyword.
  3. Final Answer:

    {x: x**2 for x in range(6) if x % 2 == 0} -> Option A
  4. Quick Check:

    Dict comprehension with condition uses 'if' and '==' [OK]
Quick Trick: Use 'if' with '==' in dict comprehension conditions [OK]
Common Mistakes:
  • Using '=' instead of '=='
  • Using 'where' instead of 'if'
  • Wrong value expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes