def _get_groups_hosts_dict(dynamic_inventory, top_level_group='all'):
    """Get a dictionary of groups and hosts. Hosts will be listed under
    their lowest level group membership only.

    Args:
        dynamic_inventory (dict): Dynamic inventory dictionary
        top_level_group (str): Name of top level group

    Returns:
        dict: Dictionary containing groups with lists of hosts
    """
    groups_hosts_dict = {}
    if 'hosts' in dynamic_inventory[top_level_group]:
        if top_level_group not in groups_hosts_dict:
            groups_hosts_dict[top_level_group] = []
        groups_hosts_dict[top_level_group] += (
            dynamic_inventory[top_level_group]['hosts'])
    if 'children' in dynamic_inventory[top_level_group]:
        for child in dynamic_inventory[top_level_group]['children']:
            groups_hosts_dict.update(_get_groups_hosts_dict(dynamic_inventory,
                                                            child))
    return groups_hosts_dict