Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Your syntax is not valid Python, see this:

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading