I have this data data =
{23: [‘WA’, ‘NV’, ‘AZ’, ‘OH’], 24: [‘NV’, ‘OH’], 25: [‘NV’, ‘OH’]}
i’m trying to get one unique values from each keys list, like this
{23: [‘WA’], 24: [‘NV’], 25: [‘OH’]}
but in my current code it return something different, which its not i’m expecting
here is my code:
def get_unique_states(data):
unique_states = {}
all_states = set()
for key, states in data.items():
unique_states[key] = []
current_states = set(states)
unique_to_current = current_states - all_states
if unique_to_current:
unique_states[key].extend(list(unique_to_current))
else:
for state in states:
if state not in unique_states.values():
unique_states[key].append(state)
break
all_states.update(current_states)
return unique_states
data = {23: ['WA', 'NV', 'AZ', 'OH'], 24: ['NV', 'OH'], 25: ['NV', 'OH']}
unique_states = get_unique_states(data)
print(unique_states)
i’m trying to keep each key value with one different list value, if there are no more unique values keep giving the first key value to it.
but the output that i getting right now is:
{23: [‘NV’, ‘AZ’, ‘WA’, ‘OH’], 24: [‘NV’], 25: [‘NV’]}
but i need this data
{23: [‘WA’], 24: [‘NV’], 25: [‘OH’]}
Thank you for your response