Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Access Nested Dictionary Items with .get()

# key present
print(D['emp1'].get('name'))
# Prints Bob

# key absent
print(D['emp1'].get('salary'))
# PrintsNone
Comment

Accessing elements from a Python Nested Dictionary

# welcome to softhunt.net
# Creating a Dictionary
Dictionary = {0: 'Softhunt', 1: '.net',
		2:{'i' : 'By', 'ii' : 'Ranjeet', 'iii' : 'Andani'}}
print('Dictionary', Dictionary)

# Accessing element using key
print(Dictionary[0])
print(Dictionary[2]['i'])
print(Dictionary[2]['ii'])
Comment

nested dict

def dict_generator(indict, pre=None):
    pre = pre[:] if pre else []
    if isinstance(indict, dict):
        for key, value in indict.items():
            if isinstance(value, dict):
                for d in dict_generator(value, pre + [key]):
                    yield d
            elif isinstance(value, list) or isinstance(value, tuple):
                for v in value:
                    for d in dict_generator(v, pre + [key]):
                        yield d
            else:
                yield pre + [key, value]
    else:
        yield pre + [indict]
Comment

PREVIOUS NEXT
Code Example
Python :: No installed app with label 
Python :: doctest example in python 
Python :: validationerror django params 
Python :: TfidfVectorizer use 
Python :: list functions 
Python :: how to add trailing zeros in python 
Python :: tkinter pack() 
Python :: flip dictionary python 
Python :: Install Pip 2 on ubuntu linux 
Python :: API curl python pandas 
Python :: sns boxplot ylabelsize 
Python :: sys module in python 
Python :: python remove item from list 
Python :: how to see truncated values in jupyter notebook 
Python :: k fold CV with xgboost 
Python :: heading none in pandas import 
Python :: find an element using id in requests-html library in python 
Python :: map to numpy array 
Python :: how to use python to download files from the interent 
Python :: simple keras model with one layer 
Python :: creating a dictionary from lists 
Python :: facebook python 
Python :: Python NumPy squeeze function Example 
Python :: python os path safe string 
Python :: list of list to numpy array 
Python :: inverse box-cox transformation python 
Python :: reverse order of dataframe rows 
Python :: WSGIPassAuthorization on 
Python :: python elementTree tostring write() argument must be str, not bytes 
Python :: remove hh:mm:ss from pandas dataframe column 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =