title: How to traverse through a dictionary of dictionaries and extract values that match list elements in python?
body: <p>I have two lists like this:</p>
  
  <pre><code>l = [ 'of', 'in', 'fly']
  
  l2 = ['starbucks', 'the', 'and', '.', ',', 'from', 'to']
  </code></pre>
  
  <p>and a dictionary like this:</p>
  
  <pre><code>d = { of : {in : 1, and : 2, to: 0}, in : { of : 2,  and : 5, to : 3}, and : {of : 3, in: 6, to: 2}, to: {and: 1, of: 0, in: 3}}
  </code></pre>
  
  <p>I want to traverse from list <code>l</code>, and if there is a key of the word in the list l in the dictionary d, then traverse list l2 and match the keys in the inner dictionary (i.e., the value for the matched key from outer dictionary) and only extract those key values from the inner dictionary, as the values for that key from outer dictionary.
  And if word is not in d, then add it to new_dict giving it a value 0.</p>
  
  <p>Like for the above I want an output like this:</p>
  
  <pre><code>new_dict = {of : [2, 0] , in : [5, 3], fly: 0}
  </code></pre>
  
  <p>since 'and' and 'to' are the only words that match with the inner dictionary.</p>
  
  <p>I was doing something like this:</p>
  
  <pre><code>for k in l:
      if k in d:
          new_d[k]=d[k]
          d_values = new_d.values()
          for k2 in l2:
              if k2 in d_values:
                  t[k2] = d_values[k2]
                  temp[k] = t
      else temp[k] = 0
  </code></pre>
  
  <p>But I'm getting <code>temp = {}</code>. Why is this so? How can I fix this?</p>