Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a nested Dictionary<> objects in a foreach loop

I have a nested Dictionary structure like this:

Dictionary<string, Dictionary<string, string>> dict;

I'm trying to access the elements using two foreach loops but the compiler won't let me use the following as the loop variable for my inner loop:

Dictionary<string, string>

This is what I have:

foreach (string key in dict.Keys) {
    foreach (Dictionary<string, string> innerDict in dict[key]) {
        // ...
    }
}

The compiler says:

Cannot convert type 'System.Collections.Generic.KeyValuePair<string,string>'
 to 'System.Collections.Generic.Dictionary<string,string>'

I can use KeyValuePair<string,string> in the inner loop, but I would like to access the dictionary object itself as a whole (so that I can do something like this: if (dict.ContainsKey(innerDict)) { ... })

like image 374
user2455816 Avatar asked Jun 05 '13 13:06

user2455816


People also ask

How do you iterate through a nested dictionary?

Iterate over all values of a nested dictionary in python For a normal dictionary, we can just call the items() function of dictionary to get an iterable sequence of all key-value pairs.

How do you break a nested dictionary in Python?

To delete an item stored in a nested dictionary, we can use the del statement. The del statement lets you delete an object. del is written like a Python break statement, on its own line, followed by the item in the dictionary that you want to delete.

How do you get a value from a nested dictionary python?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).


1 Answers

The minimum code change to fix it is like this (but see the correct approach in the next code snippet in this answer):

Dictionary<string, Dictionary<string, string>> dict;

foreach (string key in dict.Keys)
{
    foreach (var innerDict in dict[key].Select(k => k.Value))
    {
    }
}

Your problem was that when you enumerate over a dictionary, you get a sequence of Key/Value pairs. In your case, the Value was the inner dictionary, and it is that which you needed.

I'm using the Linq "Select()" operation to convert each key/value pair into just the value part, i.e. the dictionary which is the value in the key/value pair.

However, that's a long-winded and inefficient way to go about getting each inner dictionary along with its key. Here's how it should be done:

foreach (var item in dict)
{
    string key = item.Key;
    Dictionary<string, string> innerDict = item.Value;

    // Do something with key and innerDict.
}

I'm assuming that you are trying to visit each inner dictionary in turn while knowning that inner dictionary's key in the outer dictionary.

Note that I only copied the values from item.Key and item.Value into local variables to illustrate their types. You could just use item.Key and item.Value directly, of course.

If you really only want the inner dictionaries themselves (and you don't need the key for each inner dictionary), you can just do it like so (as Nuffin suggested):

foreach (var innerDict in dict.Values)
{
    // Use inner dictionary
}
like image 89
Matthew Watson Avatar answered Nov 14 '22 22:11

Matthew Watson