Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

Slightly alternate solution using sum, then converting to set to remove duplicate and finally converting back to list:

>>> {k: list(set(sum(v, []))) for k,v in my_dict.items()}

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

But if you want to maintain the order of the values, then conversion to set will not work, you will have to follow alternate approach to remove the duplicates. You can follow How do I remove duplicates from a list, while preserving order? thread for that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading