Get data from 2 columns from a CSV file. Check whether the entries are same and add the currency amount
Question: I have a data.CSV file containg the following data

> column1,column2,amount, column3
>
> name1,empId1,**34.12** ,241682-27638-CIGGNT
>
> name2,empId2,**22.14** ,241682-27638-OCGGINT
>
> name3,empId3,**18.98** ,241942-37190-GGDIV
>
> name4,empId4,**8.98** ,241942-37190-GGDIV
>
> name5,empId5,**234.098** ,241942-37190-CHYOF

The **bold** entries is the currency amount.I want to add the **column3** and
**amount** entries into a dictionary in python.But if the entry in the column3
repeates it should add the currency amount and insert it into the dictionary.

The output should be as below.

    
    
      {'241682-27638-CIGGNT':'34.12','241682-27638-OCGGINT':'22.14','241942-37190-GGDIV':'27.92','241942-37190-CHYOF':'234.098'}
    

I tried by the below code but it is just updating(overwriting the currency
amount) the previous entry(instead of adding it to the previous amount)

    
    
    import csv
    
    path = "file_path"
    
    file=open( path +"data.CSV", "r")
    reader = csv.reader(file)
    
    counterlist = {}
    
    for row in reader :
    
        #print row[3]
        #print row[2]
        if counterlist.has_key(row[3]):
    
            row[2]+=row[3].value
            counterlist.update({row[3]:row[2]})
    
         else:
             counterlist.update({row[3]:row[2]})
    
    print counterlist
    

Thanks in advance.


Answer: Use a
[defaultdict](https://docs.python.org/3/library/collections.html#collections.defaultdict):

    
    
    from collections import defaultdict
    
    # ...
    
    counterlist = defaultdict(float)
    for row in reader:
        counterlist[row[3]] += float(row[2])
    

