def filter_blackwhitelist(l, key):
    """
    Filter black/whitelist for the keys that belong to the
    subdirectory which is embedded into the nested dictionary
    structure with the given key.

    Three different cases:
    - if l is None, then return none
    - if key is None, then we are at the top-level dictionary, thus
      include all scalar keys and the first element of tuples.
    - if key is not None, then return only the keys that are tuples
      where the first element of the tuple matches the given key

    Parameters
    ----------
    l : list
        Black- or whitelist to filter
    key : scalar variable or None
        Key to filter for. See above for the behavior if key is None
    """
    if l is None:
        return None
    else:
        fl = []
        for k in l:
            if isinstance(k, tuple):
                if key is not None and k[0] == key:
                    if len(k) == 2:
                        fl.append(k[1])
                    else:
                        fl.append(k[1:])
                elif key is None:
                    fl.append(k[0])
            elif key is None:
                fl.append(k)
        if len(fl) == 0:
            return None
        else:
            return fl
