import six


def slice_dict(data, idx):
    """Slice elements in dict `data` by `idx`.

    Slices array-like objects in `data` by index `idx`. `data` can be
    tree-like with sub-dicts, where the leafs must be sliceable by `idx`.

    Parameters
    ----------
    data: dict
        dict to be sliced.
    idx: slice
        Slice index.

    Returns
    -------
    dict
        dict with same elements as in `data` with sliced by `idx`.
    """
    if isinstance(data, dict):
        data_sliced = dict()
        for key, value in six.iteritems(data):
            data_sliced[key] = slice_dict(value, idx)
        return data_sliced
    else:
        return data[idx]