Nested Dictionaries

Python · Dictionaries

Theory

A nested dictionary is a dict whose values are themselves dicts (or lists of dicts). employees = {'e1': {'name': 'Asha', 'dept': 'IT'}} maps an employee ID to a whole record. You reach inner values by chaining square brackets, each one applied to the result of the previous: employees['e1'] returns the inner dict, and employees['e1']['dept'] returns 'IT'. Assigning works the same way: employees['e1']['dept'] = 'HR' changes the inner dict in place.

This is exactly the shape of JSON, so data from APIs or json.load() usually arrives as nested dicts and lists. Two gotchas: if any key along the path is missing you get KeyError, so chain .get() with a default dict (employees.get('e9', {}).get('dept')) for optional data. And copying the outer dict with .copy() is shallow: the copy still shares the same inner dicts, so changing one changes both; use copy.deepcopy() for a fully independent copy.

Why we use it

Real data is hierarchical: an employee has an address, an order has line items, an API response has nested sections. Nested dicts model that structure naturally without flattening everything into one level.

When to use it

Use nested dicts for JSON-like, hierarchical data and records grouped by ID. When the data is really a table (same fields for every record) and you need filtering or aggregation, convert it to a pandas DataFrame with pd.DataFrame or pd.json_normalize.

How it works

  1. Evaluate employees['e1']['dept']
  2. Look up 'e1' in the outer dict
  3. Is 'e1' a key? Yes: Get the inner dict {'name': 'Asha', 'dept': 'IT'}. No: Raise KeyError: 'e1'.
  4. Look up 'dept' in that inner dict
  5. Return 'IT'

Syntax

{"outer_key": {"inner_key": "value"}}

Example

employees = {'e1': {'name': 'Asha', 'dept': 'IT'}}
employees['e1']['dept']

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