title: What is the best way to save a complex dictionary to file?
body: <p>I have a dictionary that has tuple keys and numpy array values.  I tried saving it using h5 and pickle but I get error messages.  what is the best way to save this object to file?</p>
  
  <pre><code>import numpy as np
  from collections import defaultdict
  Q =defaultdict(lambda: np.zeros(2))
  Q[(1,2,False)] = np.array([1,2])
  Q[(1,3,True)] = np.array([3,4])
  
  &gt;&gt;&gt; Q
  defaultdict(&lt;function &lt;lambda&gt; at 0x10c51ce18&gt;, {(1, 2, False): array([1, 2]), (1, 3, True): array([3, 4])})
  </code></pre>
  
  <p>np.save traceback:</p>
  
  <pre><code>---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-99-a071e1561501&gt; in &lt;module&gt;()
  ----&gt; 1 np.save('Q.npy', Q)
  
  ~/anaconda3_420/lib/python3.5/site-packages/numpy/lib/npyio.py in save(file, arr, allow_pickle, fix_imports)
      509         arr = np.asanyarray(arr)
      510         format.write_array(fid, arr, allow_pickle=allow_pickle,
  --&gt; 511                            pickle_kwargs=pickle_kwargs)
      512     finally:
      513         if own_fid:
  
  ~/anaconda3_420/lib/python3.5/site-packages/numpy/lib/format.py in write_array(fp, array, version, allow_pickle, pickle_kwargs)
      584         if pickle_kwargs is None:
      585             pickle_kwargs = {}
  --&gt; 586         pickle.dump(array, fp, protocol=2, **pickle_kwargs)
      587     elif array.flags.f_contiguous and not array.flags.c_contiguous:
      588         if isfileobj(fp):
  
  AttributeError: Can't pickle local object 'mc_control_epsilon_greedy.&lt;locals&gt;.&lt;lambda&gt;'
  </code></pre>