-2

I have a dictionary like this.

d = {'ABCS': ['12', '12', '113', '12']}

I want unique values like this

{'ABCS': ['12', '113']}

I tried set and its giving me something like this

{'ABCS': [set(['1', '2']), set(['1', '2']), set(['1', '3']), set(['1', '2'])]}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
abn
  • 1,353
  • 4
  • 28
  • 58
  • 2
    You used `set` on the individual values *in* the list, not on the whole list. Show us your code and we can help you correct it. – Martijn Pieters Feb 05 '15 at 21:04
  • possible duplicate of [Get unique values from a list in python](http://stackoverflow.com/questions/12897374/get-unique-values-from-a-list-in-python) – BartoszKP Feb 05 '15 at 21:04

1 Answers1

1

You can use the following code to achieve your desired output. It uses dictionary comprehensions to convert your list values (i.e. ['12', '12', '113', '12']) to a set, then converts it back to a list.

In [13]:

d = {'ABCS': ['12', '12', '113', '12']}
print {k: list(set(v)) for k,v in d.items()}
{'ABCS': ['12', '113']}
Andrew
  • 3,711
  • 2
  • 20
  • 17