Simplify this dictionary of lists into a single list containing all/only unique values
dictionary, list, list-comprehension, python
Solution
Use sets; you can produce the union of all values with:
set().union(*my_dict.values())
Demo:
>>> my_dict = {"a": [1, 6, 8, 4],
... "b": [2, 7, 4, 9, 13],
... "c": [9, 5, 6, 8, 11]
... }
>>> set().union(*my_dict.values())
set([1, 2, 4, 5, 6, 7, 8, 9, 11, 13])
Sets have no order (just like dictionary keys are unordered), but you stated you don't care about the order of the output.
Problem
I have a dictionary with lists as values, like this: ``` my_dict = {"a": [1, 6, 8, 4], "b": [2, 7, 4, 9, 13], "c": [9, 5, 6, 8, 11] } ``` What I want is a list with one of each of the unique items in the lists from the dictionary. So, `my_list` should be `[1, 6, 8, 4, 2, 7, 9, 13, 5, 11]`. I don't care about order. I did it like this: ``` my_list = [] for k in my_dict: for item in my_dict[k]: if item not in my_list: my_list.append(item) ``` This works, but I feel like there is a more elegant solution, perhaps using a list comprehension.