LearnFrenzy
Sign in Take a free test

Dictionaries · Dictionaries MCQ · Question 2

d[key] versus d.get(key) Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the difference between d["k"] and d.get("k") when "k" is missing?

  • A Both return None
  • B Both raise KeyError
  • C d["k"] raises KeyError; d.get("k") returns None Correct
  • D d.get("k") raises KeyError; d["k"] returns None
Correct Answer: Option C

get() also takes a default: d.get("k", 0).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links