Search code examples
pythonpython-3.xdictionarypython-itertools

Can't flatten lists and remove duplicates when it's a dict value, for each dict key?


I have the following dict:

my_dict = {'a': [['a','b','c'], ['a',1,4]], 'b':[[1,2,3], [1],[8,2,2,1]]}

(The original dict is much larger)

I want to go over all values, merge lists in one and remove duplicates, for each key.

I am doing it with the following method:

merged_dicts_list = [{k:list(set(list(chain.from_iterable(v))))} for k,v in my_dict.items()]

But I keep getting the error:

TypeError: unhashable type: 'dict'

Please advise how to flatten/merge the lists and remove the duplicates + convert back to list to avoid this error.

Finally I want to get:

[{'a': ['a','b','c',1,4]}, {'b': [1,2,3,8]}]

Solution

  • this works :

    for k, v in my_dict.items():
        my_dict[k] =  [item for sublist in v for item in sublist]
    
    

    Edit : OOPS my bad ! that did not work, now this does :

    for k, v in my_dict.items():
        my_dict[k] = list( dict.fromkeys( [item for sublist in v for item in sublist]) )
    
    

    Output :

    {'a': ['a', 'b', 'c', 1, 4], 'b': [1, 2, 3, 8]}