簡體   English   中英

將鍵和列表值添加到字典

[英]Add key and list value to a dictionary

我有這個基本列表字典{'2011': ['William', 'George', 'Nancy'], '2012': ['Joe', 'Math']}我想添加一個鍵和列表但是它不工作

這是我的代碼:

 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)

這是我正在尋找的例外 output:

{'2011': ['William', 'George', 'Nancy'], '2012': ['Joe', 'Math'], '2013': ["Alex", "Markus"]}

您的語法無效 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM