Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to use nested dictionary in Python

5 Answers

0 votes
dict = {
    0: {'key1':34, 'key2':89, 'key3':12},
    1: {'key4':72, 'key5':90}
}

print(dict)
    
    
    
'''
run:

{0: {'key1': 34, 'key2': 89, 'key3': 12}, 1: {'key4': 72, 'key5': 90}}

'''

 



answered Apr 11, 2021 by avibootz
0 votes
dict = {
    0: {'key1':34, 'key2':89, 'key3':12},
    1: {'key4':72, 'key5':90}
}

print(dict[0]['key2'])
    
    
    
'''
run:

89

'''

 



answered Apr 11, 2021 by avibootz
0 votes
dict = {
    0: {'key1':34, 'key2':89, 'key3':12},
    1: {'key4':72, 'key5':90}
}

print(dict[1])
    
    
    
'''
run:

{'key4': 72, 'key5': 90}

'''

 



answered Apr 11, 2021 by avibootz
0 votes
dict = {
    0: {'key1':34, 'key2':89, 'key3':12},
    1: {'key4':72, 'key5':90}
}

dict[2]= {'key6':176, 'key7':120}

print(dict)
    
    
    
'''
run:

{0: {'key1': 34, 'key2': 89, 'key3': 12}, 1: {'key4': 72, 'key5': 90}, 2: {'key6': 176, 'key7': 120}}

'''

 



answered Apr 11, 2021 by avibootz
0 votes
from collections import defaultdict

d = defaultdict(lambda: defaultdict(dict))

d['key_outer_1'] = {'key_inner_1':'val1', 'key_inner_5':'val5'}
d['key_outer_2'] = {'key_inner_2':'val2'}
d['key_outer_3'] = {'key_inner_3':'val3'}


print(dict(d))

    
    
    
'''
run:

{'key_outer_1': {'key_inner_1': 'val1', 'key_inner_5': 'val5'}, 'key_outer_2': {'key_inner_2': 'val2'}, 'key_outer_3': {'key_inner_3': 'val3'}}

'''

 



answered Apr 11, 2021 by avibootz
...