How to convert in the shortest way a list of Tuples to Dictionary (C#) ?
IList<Tuple<long, int>> applyOnTree = getTuples();
Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.
A tuple containing a list cannot be used as a key in a dictionary. Answer: True. A list is mutable. Therefore, a tuple containing a list cannot be used as a key in a dictionary.
Assuming the long
is the key and the int
is the value;
applyOnTree.ToDictionary(x => x.Item1, x => x.Item2);
Obviously, just reverse those two if it's the other way around.
Use ToDictionary
extension method:
var dictionary = applyOnTree.ToDictionary(l => l.Item1, l => l.Item2);
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