dict.get()

Python · Dictionaries

Theory

.get(key, default) returns the value for key if it exists, and otherwise returns default instead of raising an error. With employee = {'name': 'Asha', 'salary': 65000}, employee.get('bonus', 0) returns 0, while employee['bonus'] would raise KeyError: 'bonus'. If you leave out the default, get() returns None for missing keys. It never adds the key to the dict; the dict is unchanged after the call.

This makes get() ideal for optional fields, such as data from APIs where some records lack a key. It is also the classic counting idiom: counts[word] = counts.get(word, 0) + 1. Two subtleties: get() cannot tell a missing key apart from a key whose value is actually None, so use key in d when that distinction matters. And the default expression is evaluated every time, even when the key exists, so avoid expensive defaults. setdefault() is a cousin that also inserts the default into the dict.

Why we use it

Real data is incomplete: some customers have no phone number, some orders have no discount. get() lets your code handle missing keys gracefully with a sensible default instead of crashing.

When to use it

Use get() when a key is genuinely optional and a default makes sense. Use d[key] when the key must exist, so a missing key fails loudly. For counting or grouping many keys, collections.Counter or defaultdict is cleaner.

How it works

  1. Call employee.get('bonus', 0)
  2. Hash 'bonus' and look for it
  3. Is 'bonus' a key in the dict? Yes: Return its stored value. No: Return the default (0, or None if not given).
  4. The dict itself is not modified
  5. print shows 0

Syntax

dict.get(key, default)

Example

employee = {'name': 'Asha', 'salary': 65000}
print(employee.get('bonus', 0))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic