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 !


0 Comments

Leave a Reply

Avatar placeholder