简体   繁体   中英

Python remove duplicates in dictionary of lists

My dictionary looks something like this:

dictionary= {apple:[3,5], banana:[3,3,6], strawberry:[1,2,4,5,5]}

How am I able to remove all duplicates (so create a set) for each value/list?

I would like the new dictionary to be look this:

{apple:[3,5], banana:[3,6], strawberry:[1,2,4,5]}

using dict comprehension and sets to remove duplicates

d= {'apple':[3,5], 'banana':[3,3,6], 'strawberry':[1,2,4,5,5]}
print {k:list(set(j)) for k,j in d.items()}

results in

{'strawberry': [1, 2, 4, 5], 'apple': [3, 5], 'banana': [3, 6]}

If you want to preserve the list order

d= {'apple':[3,5,5,8,4,5], 'banana':[3,3,6,1,1,3], 'strawberry':[5,1,1,2,4,5,5]}
print {k:sorted(set(j),key=j.index) for k,j in d.items()}

results in:

{'strawberry': [5, 1, 2, 4], 'apple': [3, 5, 8, 4], 'banana': [3, 6, 1]}
for lst in dictionary.values():
    lst[:] = list(set(lst))

Going through set might change the order, though. If that must not happen, OrderedDict is an option:

for lst in dictionary.values():
    lst[:] = list(collections.OrderedDict.fromkeys(lst))

Or if the lists shall be sorted, you can do that instead:

for lst in dictionary.values():
    lst[:] = sorted(set(lst))

Or if the lists already are sorted, you could keep the first element and every element that's not a duplicate of the element before it.

for lst in dictionary.values():
    lst[:] = lst[:1] + [b for a, b in zip(lst, lst[1:]) if a != b]
 dictionary= {"apple":[3,5], "banana":[3,3,6], "strawberry":[1,2,4,5,5]}
 for key,item in dictionary.items():
     dictionary[key]=set(item)

 print(dictionary)

output:

 {'apple': {3, 5}, 'banana': {3, 6}, 'strawberry': {1, 2, 4, 5}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM