在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}
我该如何实现?
推荐答案
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屋!
查看全文