Changing dict structure from values to keys

Lets say i have the following dictionary:

my_dict = {'month': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'year': [2006, 2012, 2003, 2006, 2012, 2018, 2020, 2011, 2000, 2001, 2013, 2013]}

How can i create a new dictionary looking like this?

new_dict = {'1': '2006', '2': '2012', ..., '12': '2013'}

1 answer

  • answered 2020-03-25 13:35 Alexander Lekontsev

    dict(zip(my_dict['month'], my_dict['year']))
    

    Converting to string values:

    months = [str(item) for item in my_dict['month']]
    years = [str(item) for item in my_dict['year']]
    dict(zip(months, years))