Create dictionary from a list and append words

And i want to turn it into this dict by joining them as 1 key and given all a default value of 0 like this
guys i am trying to create this dict with a default valaue for 0

my_dict = {'2014_Q1': 0, '2014_Q2': 0, '2014_Q3': 0, '2014_Q4': 0, '2015_Q1': 0, '2015_Q2': 0, '2015_Q3': 0, '2015_Q4': 0, '2016_Q1': 0, '2016_Q2': 0, '2016_Q3': 0, '2016_Q4': 0, '2017_Q1': 0, '2017_Q2': 0, '2017_Q3': 0, '2017_Q4': 0, '2018_Q1': 0, '2018_Q2': 0, '2018_Q3': 0, '2018_Q4': 0, '2019_Q1': 0, '2019_Q2': 0, '2019_Q3': 0, '2019_Q4': 0}

 year = [2014, 2015, 2016, 2017, 2018, 2019]
 quarter = ['Q1', 'Q2', 'Q3', 'Q4']

This is my code i am not getting it right

change_it = {}
for year in years:
    for quarter in quarters:
        change_it = str(year) + '_' + str(quarter)
        change_it += change_it

Someone kindly help

Hi @ekkpodogui.

In your code, you initiated a blank dictionary called change_it, and then overwrote this variable as a string in your loop. Using the + operator with strings causes them to concatenate, so your final result is the last iteration with year 2019 and Q4 added to itself, or '2019_Q42019_Q4'.

When you’re using dictionaries, the syntax is dictionary_name[key] = value. In your example, the concatenated string from the year and quarter is going to be the key in your dictionary, and the value is 0. You should use a different variable name for your string so that it doesn’t overwrite your blank dictionary, and then use the dictionary syntax in the loop instead to add the key/value to your dictionary.

Also I had a problem running the loops, I think you meant to call the lists years and quarters.

I hope that helps. The Dictionaries and Frequencies mission on Dataquest goes over building dictionaries. It’s a more complicated scenario than your example, but it shows the syntax for building a dictionary and may be useful to know for when you’re ready to do computations with the new dictionary you’re creating.


First you wrote change_it = {} #it assigns empty dictionary to change_it variable
Then in your code inside inner for loop you wrote: change_it = str(year) + ‘’ + str(quarter) #Now change_it is not dict anymore as it just holds string concatenation in format year_quarter.
Then change_it += change_it means change_it = str(year) + '
’ + str(quarter) + str(year) + ‘_’ + str(quarter) #not what you want

Also as pointed in the previous comment, your lists are named year and quarter respectively without s at the end hence should use correct list names in for loops.

You can store each year_quarter concatenation in a year_qrtr_str variable within inner for loop instead so that change_it remains an empty dictionary. You can then do something like change_it[year_qrtr_str] = 0 #it will assign each year_qrtr_string as key to change_it dictionary with the value of 0

year_qrtr_str = str(x) + “_” +y
change_it[year_qrtr_str] = 0


Thanks guys for pointing out where am going wrong thanks i have been able to correct it