Bird
0
0

Given this dictionary:

hard📝 Application Q9 of 15
Python - Data Types as Values
Given this dictionary:
status = {"a": True, "b": False, "c": 0, "d": "1"}

Which code returns a list of keys with truthy values?
A[k for k, v in status.items() if v]
B[k for k, v in status.items() if v == True]
C[k for k, v in status.items() if bool(k)]
D[k for k, v in status.items() if v is True]
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering dictionary by truthy values

    We want keys where the value is truthy (non-zero, True, non-empty).
  2. Step 2: Evaluate each option

    [k for k, v in status.items() if v] filters by 'if v' which keeps True and "1" but excludes False and 0. [k for k, v in status.items() if v == True] keeps only values == True (excludes "1"). [k for k, v in status.items() if bool(k)] filters by key truthiness, not value. [k for k, v in status.items() if v is True] keeps only values that are exactly True (excludes "1").
  3. Final Answer:

    [k for k, v in status.items() if v] -> Option A
  4. Quick Check:

    Filter dict by 'if v' for truthy values [OK]
Quick Trick: Use 'if v' to filter truthy dict values in comprehension [OK]
Common Mistakes:
MISTAKES
  • Using 'v == True' excludes "1"
  • Filtering keys instead of values
  • Using 'v is True' excludes "1"

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes