Is it possible to slice keys in h5py using python 3, without forming a list?
Question: I am using h5py to read in data from an HDF5 file, and have found that code
which worked using Python 2 does not work using Python 3. The file is
formatted such that 2D frames of data are present as distinct datasets, which
I want to read into a 3D array. The file structure looks like this:

    
    
    file.h5
     |- groupname (group)
         |- frame1 (dataset)
         |- frame2 (dataset)
       ...
    

To read the frames into a 3D array, I have to access the first dataset to get
its shape and type information. Because I don't actually know the _exact_ name
for each frame, the code I had been using to access the first frame looked
like this:

    
    
    import h5py
    
    fid = h5py.File('file.h5', 'r')
    datagroup = fid['groupname']
    dataset0 = datagroup[datagroup.keys()[0]]
    

However, the [documentation](http://docs.h5py.org/en/latest/high/group.html)
for h5py says

> "When using h5py from Python 3, the keys(), values() and items() methods
> will return view-like objects instead of lists."

The view objects support iteration but not slicing. So to avoid an error I had
to change that line to the following:

    
    
    dataset0 = datagroup[ [k for k in datagroup.keys()][0] ]
    

which artificially constructs a temporary list and then grabs its first
element. To me this looks awful. Is there a better way to do this?


Answer: `Group` supports returning an iterator but is not directly iterable. That
leads to the following:

    
    
    dataset0 = datagroup[next(iter(datagroup))]
    

