def flatten(d, key_as_tuple=True, sep='.'):
    """ get nested dict as {key:val,...},
    where key is tuple/string of all nested keys

    Parameters
    ----------
    d : dict
    key_as_tuple : bool
        whether keys are list of nested keys or delimited string of nested keys
    sep : str
        if key_as_tuple=False, delimiter for keys

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {1:{"a":"A"},2:{"b":"B"}}
    >>> pprint(flatten(d))
    {(1, 'a'): 'A', (2, 'b'): 'B'}

    >>> d = {1:{"a":"A"},2:{"b":"B"}}
    >>> pprint(flatten(d,key_as_tuple=False))
    {'1.a': 'A', '2.b': 'B'}

    """

    def expand(key, value):
        if isinstance(value, dict):
            if key_as_tuple:
                return [(key + k, v)
                        for k, v in flatten(value, key_as_tuple).items()]
            else:
                return [(str(key) + sep + k, v)
                        for k, v in flatten(value, key_as_tuple).items()]
        else:
            return [(key, value)]

    if key_as_tuple:
        items = [item for k, v in d.items() for item in expand((k,), v)]
    else:
        items = [item for k, v in d.items() for item in expand(k, v)]

    return dict(items)