Content · Programming · Python

Python dict.get Method: Usage and Examples

dict.get(key, default=None) returns the value associated with a key, or the supplied fallback when it is absent. Unlike direct indexing, missing keys do not raise KeyError. See two practical examples, edge cases and.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

An order record might omit an optional delivery note.

What You Need to Understand


dict.get(key, default=None) returns the value associated with a key, or the supplied fallback when it is absent. Unlike direct indexing, missing keys do not raise KeyError.

When to Use It


An order record might omit an optional delivery note.

Step-by-Step Python Example


order = {"item": "Book", "quantity": 2}
print(order.get("quantity"))
print(order.get("note", "No note"))

How the Example Works


The existing quantity is returned; the missing note receives a readable fallback.

Example Output


2
No note
0

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

product = {"price": 20}
print(product.get("discount", 0))

Important Behavior and Edge Cases


A present key with value None is different from a missing key. Use key in mapping to tell them apart.

Try It Yourself


Read an optional discount field with default zero.

Common Beginner Mistakes


  • A present key with value None is different from a missing key. Use key in mapping to tell them apart.
  • Using the wrong collection method for this operation without checking whether it mutates the object, returns a result, or raises on missing data.

Key Takeaways


  • dict.get(key, default=None) returns the value associated with a key, or the supplied fallback when it is absent.
  • The existing quantity is returned; the missing note receives a readable fallback.
  • A present key with value None is different from a missing key.
Share this resourceSend this page to someone who may find it useful.