Creating a dictionary in python by combining two .csv files
<p>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 </p>

<pre><code>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)
</code></pre>

<p>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 </p>

<pre><code>{'2016-09-12': ('1835400', '45.75', '21681500', '9.78')}
</code></pre>

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

<p>You're creating a new dictionary on every iteration of your <code>for</code>, so only the update from the last iteration is kept, others have been thrown away.</p>

<p>You can solve this by moving the dictionary setup outside the <code>for</code>:</p>

<pre><code>new_dict = {}
for i in range(10):
    ...
</code></pre>

python dictionary