Home > Article > Backend Development > How to add elements to python dictionary
How to add elements to a python dictionary?
Dictionary is another mutable container model and can store any type of object.
Each key-value key=>value pair in the dictionary is separated by colon:. Each key-value pair is separated by comma,. The entire dictionary is included in curly braces {}. The format is as follows:
d = {key1 : value1, key2 : value2 }
The way to add new content to a dictionary is to add new key/value pairs.
Related recommendations: "Python Video Tutorial"
Example:
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8 # 更新 dict['School'] = "RUNOOB" # 添加 print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']
Output result of the above example:
dict['Age']: 8 dict['School']: RUNOOB
Recommended related articles:
1.How to append elements to a collection in python
2.How to add elements to an array in python
Related video recommendations:
1.2019python self-study video
The above is the detailed content of How to add elements to python dictionary. For more information, please follow other related articles on the PHP Chinese website!