title: h5py: How to index over multiple large HDF5 files without loading all their content into memory
body: <p>This is a question about working with multiple HDF5 datasets simultaneously while treating them as one dataset as far as possible.</p>
  
  <p>I have multiple <code>.h5</code> files, each of which contains tens of thousands of images. Let's call the files</p>
  
  <pre><code>file01.h5
  file02.h5
  file03.h5
  </code></pre>
  
  <p>I now want to create a list or array that contains "pointers" to all images of all three files, without actually loading the images.</p>
  
  <p>Here is what I have so far:</p>
  
  <p>I first open all files:</p>
  
  <pre><code>file01 = h5py.File('file01.h5', 'r')
  file02 = h5py.File('file02.h5', 'r')
  file03 = h5py.File('file03.h5', 'r')
  </code></pre>
  
  <p>and add their image datasets to a list:</p>
  
  <pre><code>images = []
  images.append(file01['images'])
  images.append(file02['images'])
  images.append(file03['images'])
  </code></pre>
  
  <p>where <code>file01['images']</code> is an HDF5 dataset of shape e.g. <code>(52722, 3, 160, 320)</code>, i.e. 52722 images. All good so far, none of the content has been loaded into memory yet. Now I want to make these three separate image lists into one so that I can work with it as if it were one large dataset. I tried to do this:</p>
  
  <pre><code>images = np.concatenate(images)
  </code></pre>
  
  <p>This is where it breaks. As soon as I concatenate the three HDF5 datasets, it actually loads the images as Numpy arrays and I run out of memory.</p>
  
  <p>What would be the best way to solve this?</p>
  
  <p>I need a solution that allows me to Numpy-slice and index into the three datasets as if it were one.</p>
  
  <p>For example, assume each dataset contained 50,000 images and I wanted to load the third image of each dataset, I need a list <code>images</code> that allows me to index those images as</p>
  
  <pre><code>batch = images[[2, 50002, 100002]]
  </code></pre>