i seem to write this code over and over again and wanted to see if there was a better way of doing it more generically.
I start out with a list of Foo objects
Foo[] foos = GenerateFoos();
I think want to create a dictionary where the key and value are both properties of Foo
for example:
Dictionary<string, string> fooDict = new Dictionary<string, string>(): foreach (Foo foo in foos) { fooDict[foo.Name] = foo.StreetAddress; }
is there anyway of writing this code generically as it seems like a basic template where there is an array of objects, a key property a value property and a dictionary.
Any suggestions?
I am using VS 2005 (C#, 2.0)
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.
To convert two lists to a dictionary in Python, use the zip() function, and it returns the list of tuples and passes that list of tuples to a dict() function, and it returns the dictionary.
Item[] Property. This property is used to get or set the value associated with the specified key in the Dictionary. Here, key is the Key of the value to get or set.
The first list of items will be keys for the dictionary, and the second list of items will be the dictionary. The zip (fields, values) method returns an iterator that generates two-items tuples. If you call dict () on that iterator, you can create the dictionary you need.
One important thing to note that dictionary = {zip (keys, values)} will not work. You have to declare as dict (zip ()) explicitly. If you make it work, you have to use proper dictionary comprehension.
To create a dictionary from two sequences, use the dict () and zip () method. The dict (zip (keys, values)) needs the one-time global lookup each for dict and zip. Still, it doesn’t create any needless intermediary data structures or deal with local lookups in function applications.
With LINQ:
var fooDict = foos.ToDictionary(x=>x.Name,x=>x.StreetAddress);
(and yes, fooDict
is Dictionary<string, string>
)
edit to show the pain in VS2005:
Dictionary<string, string> fooDict = Program.ToDictionary<Foo, string, string>(foos, delegate(Foo foo) { return foo.Name; }, delegate(Foo foo) { return foo.StreetAddress; });
where you have (in Program
):
public static Dictionary<TKey, TValue> ToDictionary<TSource, TKey, TValue>( IEnumerable<TSource> items, Converter<TSource, TKey> keySelector, Converter<TSource, TValue> valueSelector) { Dictionary<TKey, TValue> result = new Dictionary<TKey, TValue>(); foreach (TSource item in items) { result.Add(keySelector(item), valueSelector(item)); } return result; }
If you are using framework 3.5, you can use the ToDictionary
extension:
Dictionary<string, string> fooDict = foos.ToDictionary(f => f.Name, f => f.StreetAddress);
For framework 2.0, the code is pretty much as simple as it can be.
You can improve the performance a bit by specifying the capacity for the dictionary when you create it, so that it doesn't have to do any reallocations while you fill it:
Dictionary<string, string> fooDict = new Dictionary<string, string>(foos.Count):
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