In python I have some code that extracts data from text files, then stores each word and what file they appear in, in a dictionary. It looks something like this:
{'apple': [1, 1, 4, 5], 'carrot': [1, 2, 2], 'pear': [1, 3, 3, 6]}
but a lot bigger with a lot more files.
I want some code that will delete the duplicates in the value pairs. Right now it's telling me every time a word appears in a file, but I just want to know if it appears in each file. So I want my dictionary to look like this:
{'apple': [1, 4, 5], 'carrot': [1, 2], 'pear': [1, 3, 6]}
How would I do this?
CodePudding user response:
Try using dict comprehensions:
adict = {'apple': [1, 1, 4, 5], 'carrot': [1, 2, 2], 'pear': [1, 3, 3, 6]}
print({k: list(dict.fromkeys(v)) for k, v in adict.items()})