0

Hello I am trying to get the unique values in a dictionary. I have a working code but I wanted to see if there was a cleaner solution.

"test.csv" looks like this:

Test1,1,BALL 
Test1,2,APPLE 
Test1,3,APPLE 
Test,4,APPLE
Test,6,BALL

And the script looks like this:

name_dist=dict()
with open('test.csv','r')as f:
    spamreader=csv.reader(f,delimiter=",")
    next(f)
    for row in spamreader:
        if row[2].strip() in name_dist:
            name_dist[row[2].strip()].append(row[0].strip())
        else:
            name_dist[row[2].strip()]=[row[0].strip()]
finaldict={}
for key,value in name_dist.iteritems():
    finaldict[key]=set(value)

I want the output to be: {'APPLE': {'Test', 'Test1'}, 'BALL': {'Test'}}

Please let me know if there is a better way to get the same result.

MackM
  • 2,906
  • 5
  • 31
  • 45
Sam
  • 1,206
  • 2
  • 12
  • 27

1 Answers1

0

very new solution

from collections import defaultdict
name_dist=defaultdict(set)
with open('test.csv','r')as f:
    spamreader=csv.reader(f,delimiter=",")
    next(f)
    for row in spamreader:
        name_dist[row[2].strip()].add(row[0].strip())
print dict(name_dist) #convert it back to a normal dictionary

courtesy of this post Python: list() as default value for dictionary

Hamuel
  • 633
  • 5
  • 16