How to add elements to dictionary in python

王林
Release: 2020-10-20 09:52:56
Original
32290 people have browsed it

How to add elements to the dictionary in python: You can directly add elements to the dictionary by giving a key-value pair, such as [aa['price'] = 100 aa['price'] = 100].

How to add elements to dictionary in python

Method 1: Add directly, given key-value pair

(recommended tutorial: python video tutorial)

#pycharm
aa = {'人才':60,'英语':'english','adress':'here'}
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here'}
#添加方法一:根据键值对添加
aa['价格'] = 100
aa['价格'] = 100 # {'人才': 60, '英语': 'english', 'adress': 'here', '价格': 100}
Copy after login

Method 2: Use the update method

#添加方法二:使用update方法添加
xx = {'hhh':'gogogo'}
aa.update(xx)
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here', '价格': 100, 'hhh': 'gogogo'}
Copy after login

Related recommendations: python tutorial

The above is the detailed content of How to add elements to dictionary in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template