def filter_parameters(params, keys=None, prefix='', index=None, index_condition=None):
    """ Make a subdictionary of parameters with required keys.

    Parameter are retrieved if:
    a. It is explicitly requested (via `keys` arg).
    b. Its name starts with given prefix (defined by `prefix` arg).

    Parameters
    ----------
    params : dict
        Arguments to filter.
    keys : sequence
        Keys to retrieve.
    prefix : str, optional
        Arguments with keys starting with given prefix will also be retrieved.
        Defaults to `''`, i.e. no prefix used.
    index : int
        Index of argument value to retrieve.
        If none provided, get whole argument value.
        If value is non-indexable, get it without indexing.
    index_condition : callable
        Function that takes indexed argument value and returns a bool specifying whether should it be really indexed.
    """
    result = {}

    keys = keys or list(params.keys())
    if prefix:
        keys += [key.split(prefix)[1] for key in params if key.startswith(prefix)]

    for key in keys:
        value = params.get(prefix + key, params.get(key))
        if value is None:
            continue
        # check if parameter value indexing is requested and possible
        if index is not None and isinstance(value, list):
            # check if there is no index condition or there is one and it is satisfied
            if index_condition is None or index_condition(value[index]):
                value = value[index]
        result[key] = value
    return result