Bird
0
0

When reading a JSON file with duplicate keys, what happens to the keys in the resulting Python dictionary?

hard📝 Conceptual Q10 of 15
Python - Structured Data Files

When reading a JSON file with duplicate keys, what happens to the keys in the resulting Python dictionary?

AAn error is raised due to duplicate keys
BThe first occurrence of the key is kept, others ignored
CAll values are stored in a list under the key
DThe last occurrence of the key overwrites previous ones
Step-by-Step Solution
Solution:
  1. Step 1: Understand JSON to dict conversion behavior

    Python dictionaries cannot have duplicate keys; when duplicates exist, the last key-value pair overwrites earlier ones.
  2. Step 2: Confirm no error or list storage

    No error is raised; values are not stored as lists automatically.
  3. Final Answer:

    The last occurrence of the key overwrites previous ones -> Option D
  4. Quick Check:

    Duplicate keys in JSON: last one wins in dict [OK]
Quick Trick: Duplicate JSON keys overwrite earlier ones in Python dict [OK]
Common Mistakes:
  • Expecting error on duplicates
  • Thinking values are combined in lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes