This question already has an answer here:
(这个问题在这里已有答案:)
(在字典中添加新密钥?)
(14个答案)
I want to add an item to an existing dictionary in python.
(我想在python中将项添加到现有字典中。)
(例如,这是我的字典:)
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.
(很容易py。)
Another possible solution:
(另一种可能的方案)
default_data.update({'item3': 3})
which is nice if you want to insert multiple items at once.
(如果你想一次插入多个项目,那就太好了。)
1.4m articles
1.4m replys
5 comments
57.0k users