Kubix
Kubix

Reputation: 79

Remove duplicate from list which is used as value in dictionary

I have a dictionary with values as a list. I would like to remove duplicates from this list.

{(2,): ['a', 'a', 'b', 'b'], (3,): ['c', 'c'], (1,): ['a', 'b', 'b'], (4,): ['b', 'c', 'd']}

This dictionary should return {2:['a','b'], 1:['a','b'], 4('b','c','d')}. I was only able to find answers when there was a dictionary created or merged. In my case it will not work because of multiple inputs as lists. Is there a way to change original code or add something in the end in order to get dictionary without duplicates? My original code with which I get this dictionary is below.

from itertools import izip

a = [1,1,1,2,2,2,2,3,3,4,4,4]
b = ['a','b','b','a','a','b','b','c','c','b','c','d']

d = {} # Create dictionary

def solve(*lists):  

    for i, k in enumerate(izip(*lists)): # check and returns an iterator that combines the elements of lists into tuples
        global d                         
        d.setdefault(k, []).append(b[i])
         #If key is in the dictionary, return its value and append item from other list in the same position 
    return d      

solve(a)
print d

I'm beginner in Python so any help will be highly appreciated. The closest answer I found is Python remove duplicate value in a combined dictionary's list , but since inputs are dictionaries and not list I can't figure out what to do.

PURPOSE FROM RESEARCH PERSPECTIVE:

I have a lists of CAD drawings and letter assigned to them (this is list b). List a is a list of real objects and assigned number to them. If the same drawing and object repeat in database it's ok. The problem is when two drawings are assigned to one object. In my case if items in the same location in two list repeat it's not a problem. 3c is just a repetition not causing problems. 3c can be ignored from my study. 3c would be a situation in which we have two identical object and two identical drawings so no conflict. '2' is a number assigned to real object and 'a' is a letter for drawing. Problem is when there is one real object and two different drawing assigned to the same object. It cause mistakes in production of parts. (2, a), (2, b), is a problem.

Upvotes: 0

Views: 149

Answers (2)

Omid
Omid

Reputation: 2667

dic = {(2,): ['a', 'a', 'b', 'b'], (3,): ['c', 'c'], (1,): ['a', 'b', 'b'], (4,): ['b', 'c', 'd']}

for i in dic:
    dic[i] = list(set(dic[i]))

you get

>>> 
{(2,): ['b', 'a'], (3,): ['c'], (1,): ['b', 'a'], (4,): ['d', 'b', 'c']}
>>> 

It's because we first turn it into a set and then convert it to a list and in the process the duplicates get deleted.

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107287

You can use set :

>>> d={(2,): ['a', 'a', 'b', 'b'], (3,): ['c', 'c'], (1,): ['a', 'b', 'b'], (4,): ['b', 'c', 'd']}
>>> {i:list(set(v)) for i,v in d.items()}
{(2,): ['a', 'b'], (3,): ['c'], (1,): ['a', 'b'], (4,): ['c', 'b', 'd']}

Upvotes: 1

Related Questions