0

I have a python dictionary

graph = {'A': ['B', 'C','B'], 'E': ['C', 'D']}

I would like to remove the duplicate values from my dictionary & would like to have in this form

graph = {'A': ['B', 'C'], 'E': ['C', 'D']}

This is the sample dataset. I'm not sure for how many cases it's occurring. Can you suggest me how to do that?

newinPython
  • 313
  • 1
  • 6
  • 19

1 Answers1

1

All you need is to convert list into set and then back into list.

graph = {'A': ['B', 'C','B'], 'E': ['C', 'D']}
for idx, k in enumerate(graph):
    graph[k] = list(set(graph[k]))
print(graph)
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72