Looping over a dictionary to make multiple dictionaries
<p>I have a dictionary, dict1, and I am wanting to find a way to loop through it to isolate all the values for shepherds, collies, and poodles. I apologize a head of time if my syntax is off. I am still learning about dictionaries!</p>

<pre><code>dict1 = {
'Bob VS Sarah': {
    'shepherd': 1,
    'collie': 5,
    'poodle': 8
},
'Bob VS Ann': {
    'shepherd': 3,
    'collie': 2,
    'poodle': 1
},
'Bob VS Jen': {
    'shepherd': 3,
    'collie': 2,
    'poodle': 2
},
'Sarah VS Bob': {
    'shepherd': 3,
    'collie': 2,
    'poodle': 4
},
'Sarah VS Ann': {
    'shepherd': 4,
    'collie': 6,
    'poodle': 3
},
'Sarah VS Jen': {
    'shepherd': 1,
    'collie': 5,
    'poodle': 8
},
'Jen VS Bob': {
    'shepherd': 4,
    'collie': 8,
    'poodle': 1
},
'Jen VS Sarah': {
    'shepherd': 7,
    'collie': 9,
    'poodle': 2
},
'Jen VS Ann': {
    'shepherd': 3,
    'collie': 7,
    'poodle': 2
},
'Ann VS Bob': {
    'shepherd': 6,
    'collie': 2,
    'poodle': 5
},
'Ann VS Sarah': {
    'shepherd': 0,
    'collie': 2,
    'poodle': 4
},
'Ann VS Jen': {
    'shepherd': 2,
    'collie': 8,
    'poodle': 2
},
'Bob VS Bob': {
    'shepherd': 3,
    'collie': 2,
    'poodle': 2
},
'Sarah VS Sarah': {
    'shepherd': 3,
    'collie': 2,
    'poodle': 2
},
'Ann VS Ann': {
    'shepherd': 13,
    'collie': 2,
    'poodle': 4
},
'Jen VS Jen': {
    'shepherd': 9,
    'collie': 7,
    'poodle': 2
 }
}
</code></pre>

<p>This is what I want, for example, but to be able to loop through to make a dictionary for each dog:</p>

<p>dict_shepherd = {'shepherd': 1,3,3,3,4,1,4,7,3,6,0,2,3,3,13,9}</p>

<p>Note: I still have not touched base with pandas and prefer help without using them :) I will get to them one day.</p>

<p>You can user defaultdict as follow</p>

<pre><code>from collections import defaultdict

dict1 = {'Bob VS Sarah': {'shepherd': 1,'collie': 5,'poodle': 8},
 'Bob VS Ann': {'shepherd': 3,'collie': 2,'poodle': 1},
 'Bob VS Jen': {'shepherd': 3,'collie': 2,'poodle': 2},
 'Sarah VS Bob': {'shepherd': 3,'collie': 2,'poodle': 4},
 'Sarah VS Ann': {'shepherd': 4,'collie': 6,'poodle': 3},
 'Sarah VS Jen': {'shepherd': 1,'collie': 5,'poodle': 8},
 'Jen VS Bob': {'shepherd': 4,'collie': 8,'poodle': 1},
 'Jen VS Sarah': {'shepherd': 7,'collie': 9,'poodle': 2},
 'Jen VS Ann': {'shepherd': 3,'collie': 7,'poodle': 2},
 'Ann VS Bob': {'shepherd': 6,'collie': 2,'poodle': 5},
 'Ann VS Sarah': {'shepherd': 0,'collie': 2,'poodle': 4},
 'Ann VS Jen': {'shepherd': 2,'collie': 8,'poodle': 2},
 'Bob VS Bob': {'shepherd': 3,'collie': 2,'poodle': 2},
 'Sarah VS Sarah': {'shepherd': 3,'collie': 2,'poodle': 2},
 'Ann VS Ann': {'shepherd': 13,'collie': 2,'poodle': 4},
 'Jen VS Jen': {'shepherd': 9,'collie': 7,'poodle': 2}}


def iter_dict(dict_, result=defaultdict(list)): # mutable as default value ot reuse result over the recursion
    for k, v in dict_.items():
        if isinstance(v, dict):
            iter_dict(v)
        else:
            result[k].append(v)
    return result

print(iter_dict(dict1))
</code></pre>

<p>That will produce a dict with all expected results</p>

<pre><code>defaultdict(&lt;class 'list'&gt;, {'shepherd': [3, 4, 9, 2, 3, 1, 0, 4, 1, 6, 3, 3, 13, 3, 3, 7], 'collie': [2, 8, 7, 8, 7, 5, 2, 6, 5, 2, 2, 2, 2, 2, 2, 9], 'poodle': [1, 1, 2, 2, 2, 8, 4, 3, 8, 5, 2, 2, 4, 2, 4, 2]})
</code></pre>

python dictionary