I have a pointer containing lists, and you need a quick way to deduplicate lists.
I know how to dedup a list individually using the set () function, but in this case I want a quick way to iterate through a dict, listing each list along the path.
hello = {'test1':[2,3,4,2,2,5,6], 'test2':[5,5,8,4,3,3,8,9]}
I would like it to look like this:
hello = {'test1':[2,3,4,5,6], 'test2':[5,8,4,3,9]}
Although I do not have to have the original order of the saved lists.
I tried using such a set, but this is not entirely correct (it is not iterating correctly, and I lose the first key)
for key, value in hello.items(): goodbye = {key: set(value)} >>> goodbye {'test2': set([8, 9, 3, 4, 5])}
EDIT . Following the PM 2Ring comment below, I now fill out the dict in different ways to avoid duplication in the first place. I used to use lists, but using sets prevents the addition of duplicates by default;
>>> my_numbers = {} >>> my_numbers['first'] = [1,2,2,2,6,5] >>> from collections import defaultdict >>> final_list = defaultdict(set) >>> for n in my_numbers['first']: final_list['test_first'].add(n) ... >>> final_list['test_first'] set([1, 2, 5, 6])
As you can see, the final output is a released set, if required.