Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to get key value in nested dictionary python

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield from recursive_items(value)
        else:
            yield (key, value)

a = {'a': {1: {1: 2, 3: 4}, 2: {5: 6}}}

for key, value in recursive_items(a):
    print(key, value)
Comment

python get nested dictionary keys

example_dict.get('key1', {}).get('key2')
Comment

Nested dictionary Python

IDs = ['emp1','emp2','emp3']

EmpInfo = [{'name': 'Bob', 'job': 'Mgr'},
           {'name': 'Kim', 'job': 'Dev'},
           {'name': 'Sam', 'job': 'Dev'}]

D = dict(zip(IDs, EmpInfo))

print(D)
# Prints {'emp1': {'name': 'Bob', 'job': 'Mgr'},
#         'emp2': {'name': 'Kim', 'job': 'Dev'},
#         'emp3': {'name': 'Sam', 'job': 'Dev'}}
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

python nested object to dict

def my_dict(obj):
    if not  hasattr(obj,"__dict__"):
        return obj
    result = {}
    for key, val in obj.__dict__.items():
        if key.startswith("_"):
            continue
        element = []
        if isinstance(val, list):
            for item in val:
                element.append(my_dict(item))
        else:
            element = my_dict(val)
        result[key] = element
    return result
Comment

Nested dictionary Python

D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
     'emp2': {'name': 'Kim', 'job': 'Dev'},
     'emp3': {'name': 'Sam', 'job': 'Dev'}}
Comment

PREVIOUS NEXT
Code Example
Python :: swapping variables 
Python :: python convert string to float 
Python :: find location of max value in python list 
Python :: copy array along axis numpy 
Python :: mixpanel export api 
Python :: date and time in python 
Python :: python sort list opposite 
Python :: database with python 
Python :: urllib_errors 
Python :: python xmlrpc 
Python :: loop in python 
Python :: get first digit of number 
Python :: for i in array in range python 
Python :: xlrd documentation 
Python :: pandas df tail 
Python :: How to shift non nan values up and put nan values down 
Python :: checking length of sets in python 
Python :: sample hierarchical clustering 
Python :: numpy diag() 
Python :: what is the size of cover in facebook 
Python :: set default palette seaborn 
Python :: python list with several same values 
Python :: plot dataframe 
Python :: python responses 
Python :: python print bytes 
Python :: python call function that need args with decorator 
Python :: string slice python 
Python :: python tkinter focus on entry 
Python :: python re.split() 
Python :: convert a list to tuple 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =