Python添加新项目到字典 [英] Python add new item to dictionary
本文介绍了Python添加新项目到字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
default_data = {
'item1':1,
' item2':2,
}
我想添加新项目: p>
default_data = default_data + {'item3':3}
如何实现?
解决方案
default_data ['item3'] = 3
Easy as py。
另一个可能的解决方案:
default_data.update({'item3':3})
如果您要一次插入多个项目,那么很好。
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 new item such that:
default_data = default_data + {'item3':3}
How to achieve this?
解决方案
default_data['item3'] = 3
Easy as py.
Another possible solution:
default_data.update({'item3': 3})
which is nice if you want to insert multiple items at once.
这篇关于Python添加新项目到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文