ShaunO ShaunO - 4 days ago 6
Python Question

Only keep unique values in setdefaults dict

I'm making two tables/dicts using the setdefault() function from collections module. The first dict is created from two lists that are zipped where the first item element becomes the key and the the second elements become the values so that there can be any number of values per key. Is there a way to exclude duplicate values for each key?

keylst = ['a','a','b','b','a','b']
vallst = [1,1,2,2,3,3]

zip = [('a',1),('a',1),('b',2),('b',2),('a',3),('b',3)]
result = a[1,1,3], b[2,2,3]

desired_result = a[1,3], b[2,3]


Here's my code so far:

import collections
import xlrd

#open workbook to get values
wb1 = xlrd.open_workbook(r"S:\Bands.xlsx")
sh1 = wb1.sheet_by_index(0)

#Open a rate code workbook
wb2 = xlrd.open_workbook(r"S:\Sands.xlsx")
sh2 = wb2.sheet_by_index(0)

#make a lists

roLst = []
raLst = []

# polpulate lists

cell_range_value = 0
for value in sh1.col(9):
cell_range_value += 1
print cell_range_value

for i in range(1,cell_range_value,1):
roVal = str(sh1.cell(i,9).value)
raVal = str(sh1.cell(i,19).value)
cell_value_id = sh1.cell(i,1).value
roLst.append(roVal)
raLst.append(raVal)

#make multivalue dict1
RoRaDct = {}

for key, item in zip(roLst,raLst):
RoRaDct.setdefault(key,[]).append(item)

for key, item in RoRaDct.items():
print key, item

#make multivalue dict2

fType = []
rtLst2 = []

cell_range_value = 0
for value in sh2.col(0):
cell_range_value += 1

for i in range(1,cell_range_value,1):
fVal = str(sh2.cell(i,1).value)
rtVal = str(sh2.cell(i,0).value)
cell_value_id = sh2.cell(i,1).value
fType.append(fVal)
rtLst2.append(rtVal)

TpRtDct = {}

#check values

for key, item in zip(fType,rtLst2):
TpRtDct.setdefault(key,[]).append(item)

for key, item in TpRtDct.items():
print key, item

Answer

If you don't care about the order, you can change the setdefault to

TpRtDct.setdefault(key,set()).add(item)

Also, keep in mind that after the addition of defaultdict it is considered more readable to write

RoRaDct = defaultdict(set)

for key, item in zip(roLst,raLst):
    RoRaDct[key].add(item)
Comments