Nesting dictionary in Python

We provide effective and economically affordable online training courses for R and Python, click here for more details and course registration !

Dictionary in Python is a useful data object that stores key-value paired information. It can be nested, combined with lists or with dictionaries. In this post, we briefly introduce three nesting dictionary structures: dictionaries inside a list, lists inside a dictionary, and dictionaries inside a dictionary.

  1. Dictionaries inside a list

The most useful case of this hierarchical type is a list of user or people, in which each user or person has multiple information stored in its corresponding dictionary. The following example code shows this structure.

#create three dictionaries, storing information for three users
user_0 = {'f_name': 'wilson', 'age': 25,'gender':'male'}   
user_1 = {'f_name': 'shirley', 'age': 28,'gender':'female'}  
user_2 = {'f_name': 'miaomiao', 'age': 18,'gender':'male'} 
#create a list, storing three dictionaries
users = [user_0, user_1, user_2]
#for loop to print out each user information from list
for user in users:
    print(user)
#output
{'f_name': 'wilson', 'age': 25, 'gender': 'male'}
{'f_name': 'shirley', 'age': 28, 'gender': 'female'}
{'f_name': 'miaomiao', 'age': 18, 'gender': 'male'}

2. Lists inside a dictionary

This hierarchy exists often when the value for some keys in a dictionary have multiple elements. For example when we create a dictionary for students learning programming languages, a student is not limited to only one favorite language. In this situation, a list of programming languages can be stored in a list, which is the value for this student.

#a dictionary of lists, storing people's programming languages
prog_langs = {
        'wilson': ['Python', 'R'],
        'shirley': ['Java','Python'],
        'mico': ['R', 'C++'],
        }
#for loop printing out each person's programming languages
for person, langs in prog_langs.items():
    print("\n" + person.title() + "'s favorite languages are:")
    for lang in langs:
        print("\t" + lang.title())
#output
Wilson's favorite languages are:
 Python
 R

Shirley's favorite languages are:
 Java
 Python

Mico's favorite languages are:
 R
 C++

3. Dictionaries inside a dictionary

This type of storing structure occur when multiple key-value pair information is needed to store for the key. For example for storing detailed information for a person, we need a dictionary including age, name, occupation for each person, and this dictionary itself acts as the value for the key- this person of a parent dictionary.

#a dictionary, storing two dictionaries for two people's information
people = {'wilson': {'first': 'wilson',
                       'last': 'zhang',
                       'job': 'engineer'},
         'shirley': {'first': 'shirley',
                    'last': 'yue',
                    'job': 'nurse'},
         }
#for loop to print out information for each person in the dictionary
for person, info in people.items():
    print("\nperson: " + person)
    f_name = info['first'] + " " + info['last']
    job = info['job']

    print("\tFull name: " + f_name.title())
    print("\tOccupation: " + job.title())
#output
person: wilson
 Full name: Wilson Zhang
 Occupation: Engineer

person: shirley
 Full name: Shirley Yue
 Occupation: Nurse
        

For getting more knowledge of Python and a preview of our training course, you can watch Python tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago