Bird
0
0

You have a list of mixed values: [0, '', None, 'Python', [], [1], False, True].

hard📝 Application Q8 of 15
Python - Data Types as Values
You have a list of mixed values: [0, '', None, 'Python', [], [1], False, True].
Write a Python expression using list comprehension to create a new list containing only the truthy values.
A[x for x in values if x == True]
B[x for x in values if not x]
C[bool(x) for x in values]
D[x for x in values if x]
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering truthy values

    We want to keep only values that are truthy, so filter with 'if x'.
  2. Step 2: Analyze each option

    [x for x in values if x] filters truthy values correctly. [x for x in values if not x] filters falsy. [bool(x) for x in values] converts all to bool, not filtering. [x for x in values if x == True] checks equality to True, which misses some truthy values.
  3. Final Answer:

    Use list comprehension with 'if x' -> Option D
  4. Quick Check:

    Filter truthy with 'if x' [OK]
Quick Trick: Use 'if x' in comprehension to filter truthy values [OK]
Common Mistakes:
MISTAKES
  • Using 'if not x' to filter truthy
  • Using bool conversion without filtering
  • Checking 'x == True' which is too strict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes