DekGenius.com
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)
python get nested dictionary keys
example_dict.get('key1', {}).get('key2')
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'}}
extract specific key values from nested dictionary
>>> [val.get('phone') for val in people.values()]
['4563', '9102', '2341']
best way to access nested key in python
def safeget(dct, *keys):
for key in keys:
try:
dct = dct[key]
except KeyError:
return None
return dct
how to print a value of a key in nested dictionary python
D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
'emp2': {'name': 'Kim', 'job': 'Dev'},
'emp3': {'name': 'Sam', 'job': 'Dev'}}
print(D['emp1']['name'])
# Prints Bob
print(D['emp2']['job'])
# Prints Dev
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'])
extract specific key values from nested dictionary
>>> [people[i]['phone'] for i in people]
['9102', '2341', '4563']
Nested dictionary Python
D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
'emp2': {'name': 'Kim', 'job': 'Dev'},
'emp3': {'name': 'Sam', 'job': 'Dev'}}
print nested dictionary values in python
for account in bank_dictionary:
print("
ACCOUNT: ", account)
for subaccount in bank_dictionary[account]:
print("
Subaccount: ", subaccount)
extract specific key values from nested dictionary
l = []
for person in people:
l.append(people[person]['phone'])
>>> l
['9102', '2341', '4563']
© 2022 Copyright:
DekGenius.com