title: Finding closest key in defaultdict
body: <p>I am using defaultdicts to store lists of values where the <code>keys</code> are periods for which values could be observed. 
  When looking up from a list of all periods of interest, I would like to find the closest period in my defaultdict (NB: not all periods are stored in the defaultdict).</p>
  
  <p>As defaultdicts are not sorted however, the below approach does not return the correct value.</p>
  
  <p>Is there a different way of returning the closest available key for defaultdicts?</p>
  
  <pre><code>from collections import defaultdict
  import numpy as np
  
  def_dict = defaultdict(list)
  # entries that will be stored in the defaultdict
  reg_dict = {0: ["a", "b"], 2: ["c", "d"], 5: ["k", "h"], -3: ["i", "l"]}
  
  # store items from regular dict in defaultdict 
  for k, v in reg_dict.items():
      def_dict[k] = v
  
  # Lookup periods
  periods = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
  
  for period in periods:
  
      # this approach does not return the right keys as defaultdicts are not sorted
      closest_key = np.abs(np.array(list(def_dict.keys())) - period).argmin()
  
      print("period: ", period, " - looked up key: ", closest_key)
  </code></pre>
  
  <p>This returns the following:</p>
  
  <pre><code>period:  -1  - looked up key:  0
  period:  0  - looked up key:  0
  period:  1  - looked up key:  0
  period:  2  - looked up key:  1
  period:  3  - looked up key:  1
  period:  4  - looked up key:  2
  period:  5  - looked up key:  2
  period:  6  - looked up key:  2
  period:  7  - looked up key:  2
  period:  8  - looked up key:  2
  </code></pre>