在 Python 中向字典添加新项目 [英] Add a new item to a dictionary in Python
本文介绍了在 Python 中向字典添加新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Python 中的现有字典中添加一个项目.例如,这是我的字典:
I want to add an item to an existing dictionary in Python. For example, this is my dictionary:
default_data = {
'item1': 1,
'item2': 2,
}
我想添加一个新项目,以便:
I want to add a new item such that:
default_data = default_data + {'item3':3}
我怎样才能做到这一点?
How can I achieve this?
推荐答案
default_data['item3'] = 3
像py一样简单.
另一种可能的解决方案:
Another possible solution:
default_data.update({'item3': 3})
如果您想一次插入多个项目,这很好.
which is nice if you want to insert multiple items at once.
这篇关于在 Python 中向字典添加新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文