def unlist_incexcl(d, include_keys=None, exclude_keys=None):
    """Unlist elements in a document.

    If there is 1 value in the list, set the element to that value.  Otherwise,
    leave the list unchanged.

    By default, traverse all keys
    If include_keys is specified, only traverse the list from include_keys a.b, a.b.c
    If exclude_keys is specified, only exclude the list from exclude_keys

    :param d: a dictionary to unlist
    :param include_keys: only unlist these keys (optional)
    :param exclude_keys: exclude all other keys except these keys (optional)
    :return: generate key, value pairs
    """
    def unlist_helper(d, include_keys=None, exclude_keys=None, keys=None):
        include_keys = include_keys or []
        exclude_keys = exclude_keys or []
        keys = keys or []
        if isinstance(d, dict):
            for key, val in d.items():
                if isinstance(val, list):
                    if len(val) == 1:
                        path = '.'.join(keys + [key])
                        if include_keys:
                            if path in include_keys:
                                d[key] = val[0]
                        elif path not in exclude_keys:
                            d[key] = val[0]
                elif isinstance(val, dict):
                    unlist_helper(val, include_keys, exclude_keys, keys + [key])
    unlist_helper(d, include_keys, exclude_keys, [])
    return d