Bird
0
0

You have a list of user info dictionaries:

hard📝 Application Q15 of 15
Python - Structured Data Files
You have a list of user info dictionaries:
users = [
  {"id": 1, "name": "Anna", "active": True},
  {"id": 2, "name": "Ben", "active": False},
  {"id": 3, "name": "Cara", "active": True}
]

How can you create a JSON string that only includes users who are active?
AConvert users to string and replace 'False' with 'True'
BUse json.dumps(users) and then remove inactive users from the string manually
CUse json.loads on users to filter active users
DUse json.dumps with a filtered list: json.dumps([u for u in users if u["active"]])
Step-by-Step Solution
Solution:
  1. Step 1: Filter the list for active users

    Use a list comprehension to select only dictionaries where "active" is True.
  2. Step 2: Convert the filtered list to JSON string

    Pass the filtered list to json.dumps to get a JSON string with only active users.
  3. Final Answer:

    Use json.dumps with a filtered list: json.dumps([u for u in users if u["active"]]) -> Option D
  4. Quick Check:

    Filter then dump to JSON string [OK]
Quick Trick: Filter list first, then convert to JSON string [OK]
Common Mistakes:
  • Trying to filter after converting to JSON string
  • Using json.loads on a Python list (wrong usage)
  • Replacing text in JSON string instead of filtering data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes