Dictionary name [key name] = new value Modify the value of the dictionary

37. Dictionary name [key name] = new value Modify the value of the dictionary

1. Mutability of dictionaries

Strings and tuples are immutable data types.

Lists and dictionaries are mutable.

Variable means that it can be changed, that is, elements can be added to lists and dictionaries, and existing elements can be deleted and modified.

2. Knowledge Review: Modifying the Elements of a List

To modify an element in the list, we can reassign that element.

Similarly, modify the dictionary is also.

Reassigning a value in the dictionary completes the modification.

[Modify the elements in the list]

listname[index] = new element content

The syntax for modifying list elements consists of 5 parts:

  1. list name
  2. Brackets []
  3. the index of the element to modify
  4. equal sign =
  5. new element content

[Code Example]

# create list name_list
name_list = ['Zhang San', 'Li Si']

# Modify an existing element in the list with listname[index] = new element content
# name_list[1]="Wang Wu"
name_list[1]="Wang Wu"

# output name_list list
print(name_list)

[Terminal output]

['Zhang San', 'Wang Wu']

The above code uses the reassignment method to replace the second element of the list, Li Si, with Wang Wu.

3. The syntax for modifying the value of a dictionary

Modify elements of a list: listname[index] = new element content

Modify the value of the dictionary: dictionary name [key name] = new value

[Kind tips]

If the value to be modified is not a single string, integer, or floating-point number, but a dictionary, tuple, list, etc. with multiple elements, it is necessary to replace all of them.

4. Code example

# create a new dictionary
code_dict = {
    "Beijing":100000,  
    "Shanghai":200000, 
    "Guangzhou":510000
}

# Modify the value corresponding to the key name `Beijing` to 900000
code_dict['Beijing']=900000

# View the modified dictionary
print(code_dict) 

[Terminal output]

{'Beijing': 900000, 'Shanghai': 200000, 'Guangzhou': 510000}

[Code Analysis]

code_dict['Beijing']=900000 means to change the value of Beijing in the first key-value pair in the dictionary to 900000.

code_dict is the dictionary name.

'Beijing' is the key name used to locate the value to be modified.

[Output result]

The value corresponding to Beijing in the original dictionary is 100000.

After running the code, the program modifies the corresponding value of Beijing in the dictionary to 900000.

5. After class practice

Create a new dictionary info_dict, the dictionary has 3 key-value pairs, the content is as follows:

  1. Basic information: Zhang San, male, 1990
  2. Hobbies: Basketball, Guitar, Python
  3. Contact information: phone number: 18088889999, email: 77997799@qq.com

[Task 1]

  1. Change male to female in the first key-value pair of the dictionary.

  2. Change the year 1990 in the first key-value pair of the dictionary to 1980.

[Reference answer]

info_dict = {
    'Basic Information':('Zhang San','male','1990 year'),
    'hobby':['basketball','Guitar','Python'],
    'contact details':{'phone number':18088889999,'Mail':'77997799@qq.com'}
}

info_dict['Basic Information']=('Zhang San','Female','1980 year')

# View dictionary
print(info_dict)

[Terminal output]

{'Basic Information': ('Zhang San', 'Female', '1980 year'), 'hobby': ['basketball', 'Guitar', 'Python'], 'contact details': {'phone number': 18088889999, 'Mail': '77997799@qq.com'}}

[Summary] To modify the value of the dictionary, no matter what data type the value of the dictionary is, you can directly assign it.

The data type of the value to be modified by the above code is a tuple, so it needs to be replaced in its entirety.

info_dict['Basic Information']=('Zhang San','Female','1980') The value of the dictionary is directly modified by the assignment method.

[Task 2]

Change the phone number 18088889999 to 123456789.

info_dict = {
    'Basic Information':('Zhang San','male','1990 year'),
    'hobby':['basketball','Guitar','Python'],
    'contact details':{'phone number':'18088889999','Mail':'77997799@qq.com'}
}

info_dict['contact details']={'phone number':'123456789','Mail':'77997799@qq.com'}

# View dictionary
print(info_dict)

[Terminal output]

{'Basic Information': ('Zhang San', 'male', '1990 year'), 'hobby': ['basketball', 'Guitar', 'Python'], 'contact details': {'phone number': '123456789', 'Mail': '77997799@qq.com'}}

The data type of the value to be modified here is a dictionary. Note that the entire replacement is required, and the phone number cannot be modified alone.

[Error code example]

info_dict = {
    'Basic Information':('Zhang San','male','1990 year'),
    'hobby':['basketball','Guitar','Python'],
    'contact details':{'phone number':'18088889999','Mail':'77997799@qq.com'}
}

info_dict['contact details']='123456789'

# View dictionary
print(info_dict)

[Terminal output]

{'Basic Information': ('Zhang San', 'male', '1990 year'), 'hobby': ['basketball', 'Guitar', 'Python'], 'contact details': '123456789'}

If the number is modified alone, what the program understands is to replace the entire dictionary with the string '123456789'.

6. Summary

Tags: Python numpy programming language

Posted by buzzby on Mon, 24 Oct 2022 07:07:08 +0300