[英]Add key and list value to a dictionary
I have this basic list dictionary {'2011': ['William', 'George', 'Nancy'], '2012': ['Joe', 'Math']}
that I want to add a key and list but it doesn't work我有这个基本列表字典
{'2011': ['William', 'George', 'Nancy'], '2012': ['Joe', 'Math']}
我想添加一个键和列表但是它不工作
here my code:这是我的代码:
my_dict = {"2011": ["William", "George", "Nancy"], "2012": ["Joe", "Math"]}
new_years = "2013"
new_name = ["Alex", "Markus"]
my_dict.update[new_years: new_name]
print(my_dict)
Here is the excepted output that I am looking for:这是我正在寻找的例外 output:
{'2011': ['William', 'George', 'Nancy'], '2012': ['Joe', 'Math'], '2013': ["Alex", "Markus"]}
Your syntax is not valid Python, see this:您的语法无效 Python,请参阅:
my_dict = {"2011": ["William", "George", "Nancy"], "2012": ["Joe", "Math"]}
new_years = "2013"
new_name = ["Alex", "Markus"]
my_dict.update({new_years: new_name})
# or my_dict[new_years] = new_name
print(my_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.