Can the value of the python dictionary be modified?

The value of a dictionary in python can be modified. First of all, we have to know what a modified dictionary is

Modify dictionary

The way to add new content to the dictionary is to add new key/value pairs, modify or delete existing key/value pairs as follows:

# ! /usr/bin/python
dict ={'Name':'Zara','Age':7,'Class':'First'};
dict['Age']=8; # update existing entry
dict['School']="DPS School"; # Add newentry
print "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];

The output of the above example:

dict['Age']:8
dict['School']: DPS School

When the key in the dictionary exists, you can access the value corresponding to the changed key in the dictionary through the dictionary name + subscript. If the key does not exist, an exception will be thrown. If you want to directly add elements to the dictionary, you can directly add dictionary elements by using dictionary name + subscript + value. Only write the key and want to assign the key later this way will throw an exception.

  a =['apple','banana','pear','orange']
  a
[' apple','banana','pear','orange']
  a ={1:'apple',2:'banana',3:'pear',4:'orange'}
  a
{1:' apple',2:'banana',3:'pear',4:'orange'}
  a[2]'banana'
  a[5]Traceback(most
recent
call
last):
File
"< pyshell#31 ", line
1, in< module  
a[5]
KeyError:5
  a[6]='grap'
  a
{1:' apple',2:'banana',3:'pear',4:'orange',6:'grap'}

Example extension:

Use the updata method to add the key-value pairs with corresponding keys in the dictionary to the current dictionary a

{1:' apple',2:'banana',3:'pear',4:'orange',6:'grap'}
 a.items()dict_items([(1,'apple'),(2,'banana'),(3,'pear'),(4,'orange'),(6,'grap')])
 a.update({1:10,2:20})
 a
{1:10,2:20,3:' pear',4:'orange',6:'grap'}
#{1:10,2:20} Replaced{1:'apple',2:'banana'}

So far, this article on whether the value of the python dictionary can be modified is introduced. For more related python dictionary values, whether the value can be modified, please search ZaLou.Cn

Recommended Posts

Can the value of the python dictionary be modified?
Can Python implement the structure of the stack?
How does python call the key of a dictionary
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
The usage of wheel in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
Detailed usage of dictionary in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Learn the basics of python interactive mode
Python3 dictionary
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Where is the pip path of python3
The essence of Python language: Itertools library
Can variable types be declared in python
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development
Can python code be made into software
What is the function body of python
What kind of work can python do
The specific method of python import library
Solve the conflict of multiple versions of python
What is the function of adb in python
Detailed explanation of the principle of Python super() method
The difference between the syntax of java and python
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Python implements the source code of the snake game
Python iterators can also be played like this
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
The consequences of uninstalling python in ubuntu, very
Python writes the game implementation of fishing master
7 features of Python3.9
2.1 The Python Interpreter (python interpreter)
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
What is the advantage of python over corporate language
A brief summary of the difference between Python2 and Python3