title: Creating a Nested Dictionary from For Loop
body: <p>I'm trying to create a new dictionary from existing data. I'm not sure the best way to explain so here is an example:</p>
  
  <p>I have the following data:</p>
  
  <pre><code>list_1 = ['aa', 'bb', 'cc']
  
  dict_1 = {'key_1': {1: 'data1', 2: 'aa'},
            'key_2': {1: 'data2', 2: 'aa'},
            'key_3': {1: 'data3', 2: 'cc'},
            'key_4': {1: 'data4', 2: 'bb'},
            'key_5': {1: 'data5', 2: 'bb'},
            'key_6': {1: 'data6', 2: 'cc'}}
  </code></pre>
  
  <p>I want to loop through list_1 and dict_1 and if the item in list_1 matches the second key value pair in dict_1 it adds it to the new dict.</p>
  
  <p>So the output would be something like:</p>
  
  <pre><code>{'aa': {{1: 'data1', 2: 'aa'},
          {1: 'data2', 2: 'aa'}},
  'bb': {{1: 'data4', 2: 'bb'},
         {1: 'data5', 2: 'bb'}},
  'cc': {{1: 'data3', 2: 'cc'},
         {1: 'data6', 2: 'cc'}}}
  </code></pre>
  
  <p>Is there a simple way to do this?</p>