I have a dictionary which has multiple values for a single key
myDict = {1: {'id1', 'id2', 'id3'}, 2: {'id4', 'id5'}, 3: {'id6'}}
My desired output is
myDict = {1: {'id1'}, 2: {'id4'}, 3: {'id6'}}
How do I only keep the first key and remove the rest?
As comments mention, there is no "first" value in a set. However, there are several ways to get one item from a set.
You could use
next(iter(v))to get an arbitrary value in O(1) time. Eg:This approach may choose a different item each time it is run. If you need to produce a consistent result, you could use eg
min, as long as you're working with strings: