Creating a dictionary in python by combining two .csv files
Question: I am trying to create a dictionary in python by combining data from two .csv
files, by matching the first column of the two files. This is what I have so
far

    
    
    import csv
    
    with open('a.csv', 'r') as my_file1 :
        rows1 = list(csv.reader(my_file1))
    with open('aa.csv', 'r') as my_file2 :
        rows2 = list(csv.reader(my_file2))
    max_length = min(len(rows1), len(rows2))
    
    for i in range(10):
        new_dict = {}
        if (rows1[i][0]== rows2[i][0]):
            temp = {(rows1[i][0], (rows1[i][5], rows1[i][6],  rows2[i][5],  rows2[i][6] )) }
            new_dict.update(temp)
    print(new_dict)
    

The output that I get is the last data entry in arrays. It does not seem to
append all the values. This is what the output looks like

    
    
    {'2016-09-12': ('1835400', '45.75', '21681500', '9.78')}
    

instead of the complete list with keys and values. How do I go about
correcting this? Thanks!


Answer: You're creating a new dictionary on every iteration of your `for`, so only the
update from the last iteration is kept, others have been thrown away.

You can solve this by moving the dictionary setup outside the `for`:

    
    
    new_dict = {}
    for i in range(10):
        ...
    

