Home >Backend Development >Python Tutorial >How to add elements to dictionary in python

How to add elements to dictionary in python

王林
王林Original
2020-10-20 09:52:5632320browse

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}

Method 2: Use the update method

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

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!

Statement:
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