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

extract specific key values from nested dictionary

>>> [val.get('phone') for val in people.values()]
['4563', '9102', '2341']
Comment

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
Comment

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
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

extract specific key values from nested dictionary

>>> [people[i]['phone'] for i in people]
['9102', '2341', '4563']
Comment

Nested dictionary Python

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

print nested dictionary values in python

for account in bank_dictionary:
    print("
 ACCOUNT: ", account)
    for subaccount in bank_dictionary[account]:
        print("
 Subaccount: ", subaccount)
Comment

extract specific key values from nested dictionary

l = []
for person in people:
    l.append(people[person]['phone'])

>>> l
['9102', '2341', '4563']
Comment

PREVIOUS NEXT
Code Example
Python :: print colored text in python 
Python :: pandas get group 
Python :: python remove blanks from string 
Python :: python how to show package version 
Python :: sort folders content by name python 
Python :: string slices 
Python :: selenium set chrome executable path 
Python :: convert to datetime object 
Python :: python comment 
Python :: python exception 
Python :: move file python os 
Python :: python pandas table save 
Python :: df empty python 
Python :: python pop 
Python :: count elements in columns pandas 
Python :: fibonacci 
Python :: Python Tkinter Message Widget 
Python :: import all csv as append dataframes python 
Python :: Converting categorical feature in to numerical features 
Python :: how to repeat if statement in python 
Python :: python script in excel 
Python :: Python check if all elements exist in another list 
Python :: django get query parameters 
Python :: pandas change period to daily frequency 
Python :: numpy delete 
Python :: tuple and list in python 
Python :: Python Remove all occurrences of a character from a string 
Python :: python get array length 
Python :: markers seaborn 
Python :: custom attribute selenium 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =