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.