I have a Dictionary that has a signature: Dictionary<int, List<string>>
. I'd like to convert it to a Lookup with a signature: Lookup<int, string>
.
I tried:
Lookup<int, string> loginGroups = mapADToRole.ToLookup(ad => ad.Value, ad => ad.Key);
But that is not working so well.
The straight answer is NO. You can not have duplicate keys in a dictionary in Python.
The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.
The answer is: yes.
You could use:
var lookup = dictionary.SelectMany(p => p.Value
.Select(x => new { p.Key, Value = x}))
.ToLookup(pair => pair.Key, pair => pair.Value);
(You could use KeyValuePair
instead of an anonymous type - I mostly didn't for formatting reasons.)
It's pretty ugly, but it would work. Can you replace whatever code created the dictionary to start with though? That would probably be cleaner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With