Python Foundation: Dictionary Dry Goods Coming!!!

Posted by RadGH on Tue, 04 Feb 2020 02:28:29 +0100

Python Foundation for Personal Phone Profile (4): Dictionary

Dictionary Definition

Definition: In Python, a dictionary is a series of key-value pairs, with each key associated with a value.

In Python, a dictionary encloses a series of key-value pairs in curly braces. You can store as many key-value pairs as you want in the dictionary, separated by commas.

people = {'name': "xiaowang",'age': 18,'points': 85}

Dictionary Operation

The dictionary operations described here are access, traverse, add, modify, delete


To access a value in a dictionary, we simply put the key corresponding to that value in [], as follows:

people = {'name': "xiaowang",'age': 18,'points': 85}

#Print results


If we need to get all the elements in a dictionary, then we have to use an efficient method to get them, and then we can use a for loop to iterate through the dictionary

1. Traverse through all key-value pairs

user = {'name':'jery','hair':'black','lover':'tom'}
for key,value in user.items():

#Print results



Case Study:
(1) To get the key-value, we need to declare that two variables store the key-value pair to get the key and value, that is, the key and value declared in the for loop in the case
(2) Since we are traversing the keys and values of all key-value pairs, we use the method items () to return a key-value pair

2. Traverse all keys

user = {'name':'jery','hair':'black','lover':'tom'}
for key in user.keys():

#Print results

Case Study:
(1) When we only need to get the middle key of a key-value pair, we only need to use the method keys()
(2) Because we only need to get the middle key of the key-value pair, we only need to declare a variable in the for loop to store the information for each key.

3. Traverse all values

user = {'name':'jery','hair':'black','lover':'tom'}
for value in user.values():

#Print results

Case Study:
(1) When we only need to get the middle value of a key-value pair, we need to use the method values()
(2) Because we only need to get the middle key of the key-value pair, we only need to declare a variable in the for loop to store information about each value.

Add key-value pairs

A dictionary is a dynamic structure that allows you to add key-value pairs at any time by specifying the dictionary name, putting keys in [], and assigning the associated values as follows:

#Add a new key-value pair to the dictionary,'home':'beijing'and'food':'apple'
people = {'name':'xiaowang','age':18,'points':85}
people['home'] = 'beijing'
people['food'] = 'apple'

#Print results
{'name': 'xiaowang', 'age': 18, 'points': 85, 'home': 'beijing', 'food': 'apple'}

Note: Key-worth ordering does not necessarily mean adding order, because Python does not care about key-worth ordering, only about keys and worth associating relationships

Modify key-value pairs

If we want to modify the values in the dictionary, we just need to associate the keys with the new values.

Because the assignment of a new value will overwrite the original value to achieve the purpose of modification.
As follows:

people = ['name':'xiaowang']
print('My name is '+people['name'])
people['name'] = 'liuliu'

#Print results
My name is xiaowang

Delete key-value pairs

For information that is not needed in a dictionary, we can delete it using a del statement.When using the Del statement, you need to specify the dictionary name and the key to delete

As follows:

#Delete'points':85 and print out the new dictionary
people = {'name':'xiaowang','age':18,'points':85}
del people['points']

#Print results

Nested application of dictionaries

There are three common nested applications of dictionaries listed below. As these three nested ideas are easy to understand through case studies, they are not explained too much, but they still need to be practiced and consolidated in order to be proficient.

1. Dictionary List

user_1 = {'name':'mark','hair':'long'}
user_2 = {'name':'jack','hair':'short'}
user_3 = {'name':'jenny','hair':'long'}

users = [user_1,user_2,user_3]
for user in users:

#Print results

2. Nest lists in dictionaries

users_lists = {
for name,foods in users_lists.items():
	print('\n'+name.title() + "'s favourite food are:")
	for food in foods:

#Print results

Xiaoming's favourite food are:

Xiaoliu's favourite food are:

Case Study:
Keys in a dictionary are unique, but values are stored as lists, so getting a value requires a for loop to iterate through the fetch

3. Nesting dictionaries in dictionaries

If we set two dictionaries as a dictionary and b dictionary, then b dictionary is nested in a dictionary, then b dictionary will be used as the value of a dictionary key-value pair

users_lists = {
		'name' = 'xiaoming',
		'hair' = 'long'
		'name' = 'xiaowang',
		'hair' = 'short'
for user,user_info in users_lists.items():
	message = user_info['name'].title+ 'has a '+user_info['hair']+'hair'

#Print results

User_number: user_1
	Xiaoming has a long hair

User_number: user_2
	Xiaowang has a short hair

Case Study:
First, we define a dictionary, users_lists, which contains two keys, user_1 and user_2.
(2) Each key is associated with a dictionary containing name and hair
(3) We first iterated through the dictionary users_lists with a for loop, and stored the keys in user and the values (dictionaries) in user_info.
(4) Next output key information [print('\nUser_number:'+user)]
Extract information from a nested dictionary to make up the content we want message [message = user_info['name']. title+'has a'+user_info['hair']+'hair']
Print message
Repeat the for loop statement until the end


Every time you compliment me, I seriously think I like it~~

Four original articles were published. 8 were praised. 565 visits were received
Private letter follow

Topics: Python