

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access nested Python dictionary items via a list of keys?
The easiest and most readable way to access nested properties in a Python dict is to use for loop and loop over each item while getting the next value, until the end.
example
def getFromDict(dataDict, mapList): for k in mapList: dataDict = dataDict[k] return dataDict a = { 'foo': 45,'bar': { 'baz': 100,'tru': "Hello" } } print(getFromDict(a, ["bar", "baz"]))
Output
This will give the output −
100
- Related Questions & Answers
- How to get a list of all the keys from a Python dictionary?
- Get dictionary keys as a list in Python
- How to create Python dictionary from list of keys and values?
- How to sort a nested Python dictionary?
- Python - Convert list of nested dictionary into Pandas Dataframe
- How to create nested Python dictionary?
- Python – Limit the values to keys in a Dictionary List
- How to recursively iterate a nested Python dictionary?
- Recursively list nested object keys JavaScript
- Properties of Dictionary Keys in Python
- Python - Cumulative Mean of Dictionary keys
- Python - Create nested list containing values as the count of list items
- C# program to get the List of keys from a Dictionary
- How to sort a dictionary in Python by keys?
- How to print all the keys of a dictionary in Python?
Advertisements