Take the 2-minute tour ×

I have a Dictionary with 5 different doubles in the list. I know the order of each item in the List. I am trying to find a one liner piece of code where I can lookup a specific value in my list given the key.

So something like:

double myDbl = myDict["key"][value[3]];

Is something like this possible, I cant find it anywhere. Thanks

share|improve this question
Have you declared the dictionary as Dictionary<string, List<double>> ? –  Alexander Nov 14 '12 at 15:39
You're question's very unclear at the moment. We have little idea how your dictionary is declared, or what the results of your experimentation have been. –  Jon Skeet Nov 14 '12 at 15:40
Problem solved, needed just a [3] instead of looking for the value property of the dictionary –  Ron Nov 14 '12 at 15:44
Why are people so concerned with writing oneliners that don't really benefit the readability or performance of the code? Write a method that checks that the key exists in the dictionary, and will fetch the value for you. Then guess what: that method is your one-liner. –  Siege Nov 14 '12 at 15:44

6 Answers 6

up vote 5 down vote accepted

As others have said, if this is a Dictionary<string, List<double>> you could just use

double value = myDict["key"][3];

However, this line:

I know the order of each item in the List.

makes me think that actually you should restructure your code. If you know that the first item always represents one piece of data (e.g. weight), the next always represents another (e.g. height) etc, then you should just create a new type with those properties. Using a List<double> for this will make the code much harder to maintain... it doesn't naturally reveal the information about what each value means.

Once you've changed it to, say, a Dictionary<string, Person> you can use:

double height = myDict["key"].Height;

which is significantly clearer.

Of course it's possible that you meant something else by the line I've quoted...

share|improve this answer
+1 for cutting through the murk of the question to the heart of the beast. (Maybe.) –  JDB Nov 14 '12 at 15:50

Assuming you have a dictionary defined as


And assuming the dictionary elements are initialized correctly you can do the following:

myDbl = myDict["key"][3];
share|improve this answer

It should probably be something like:

double myDbl = myDict["key"][3];

As you're looking for index 3 from the resulting list from the key

By understanding the order in which the operators are executed, you can see that myDict["key"] will return your value type, which here is assumed to be List<double>. From this return value, you then want to get the 4th object (0 based) so you use [3] at the end.

share|improve this answer


Dictionary<string, List<double>> l_dict = new Dictionary<string, List<double>>();
l_dict.Add( "key", new List<double>( new double[]{ 1, 2, 3, 4, 5 } );


List<double> l_valueCollection = l_dict["key"];
double l_value = l_valueCollection[3];

Or, more succinctly (the "one-liner" you wanted):

double l_value = l_dict["key"][3];
share|improve this answer
Thanks, I was close! I kept trying to look for a property "Value" and then look up the index. That was easy. Thanks –  Ron Nov 14 '12 at 15:43
@Ron - Also see Jon Skeet's answer. You may find it more helpful. Don't forget to accept an answer. –  JDB Nov 14 '12 at 15:46

If your collection looks like this:

Dictionary<string,List<double>> myDictionary;

Then the following will work:

double myDouble = myDictionary["key"][3];
share|improve this answer

Create a method like this that returns your value, no matter what key you pass in.

    private double GetValue(Dictionary<string, double> dict, string key)
        return (from pair in dict where pair.Key == key select pair.Value).FirstOrDefault();

Remember that this is using LINQ and will only work with .Net Framework 3.5

share|improve this answer
Why would you want to turn an O(1) operation into an O(n) operation? Using TryGetValue will allow for the same result much more efficiently. Oh, and LINQ doesn't need .NET 4.0 - it was introduced in 3.5. –  Jon Skeet Nov 14 '12 at 16:14
@JonSkeet Because doing it like this is better practice and the code is at the same place. And yes LINQ was in 3.5 my mistake. –  Azhar Khorasany Nov 14 '12 at 16:17
This is absolutely not better practice. I wish I could downvote multiple times. You've got a dictionary, which is optimized for lookup by key: if all you want to do is fetch the value associated with a key, you should not iterate over all entries. Note that also, you haven't even addressed the question - which was admittedly somewhat unclear - where the value isn't a double but a List<double>. –  Jon Skeet Nov 14 '12 at 16:38

Your Answer


