Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM
Machine Learning
Machine Learning
Machine Learning & AI
Manage Data
MLOps
Natural Language Processing
NumPy
Pandas
Polars
PySpark
Python Tips
Python Utilities
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Double dict.get: Get Values in a Nested Dictionary with Missing Keys

Table of Contents

Double dict.get: Get Values in a Nested Dictionary with Missing Keys

You can use an if-else statement to get values in a nested dictionary with missing keys, but it is long and hard to read. 

A better way is to use the get method twice like below.

The first get method will return an empty dictionary if the key attr doesn’t exist. The second get method will return unknown if the key color doesn’t exist.

Link to my previous tips on Python dictionary.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran