def unflatten_list(flat_dict, separator='_'):
    """
    Unflattens a dictionary, first assuming no lists exist and then tries to
    identify lists and replaces them
    This is probably not very efficient and has not been tested extensively
    Feel free to add test cases or rewrite the logic
    Issues that stand out to me:
    - Sorting all the keys in the dictionary, which specially for the root
    dictionary can be a lot of keys
    - Checking that numbers are consecutive is O(N) in number of keys

    :param flat_dict: dictionary with no hierarchy
    :param separator: a string that separates keys
    :return: a dictionary with hierarchy
    """
    _unflatten_asserts(flat_dict, separator)

    # First unflatten the dictionary assuming no lists exist
    unflattened_dict = unflatten(flat_dict, separator)

    def _convert_dict_to_list(object_, parent_object, parent_object_key):
        if isinstance(object_, dict):
            for key in object_:
                if isinstance(object_[key], dict):
                    _convert_dict_to_list(object_[key], object_, key)
            try:
                keys = [int(key) for key in object_]
                keys.sort()
            except (ValueError, TypeError):
                keys = []
            keys_len = len(keys)

            if (keys_len > 0 and sum(keys) ==
                int(((keys_len - 1) * keys_len) / 2) and keys[0] == 0 and
                    keys[-1] == keys_len - 1 and
                    check_if_numbers_are_consecutive(keys)):

                # The dictionary looks like a list so we're going to replace it
                parent_object[parent_object_key] = []
                for key_index, key in enumerate(keys):
                    parent_object[parent_object_key].append(object_[str(key)])
                    # The list item we just added might be a list itself
                    # https://github.com/amirziai/flatten/issues/15
                    _convert_dict_to_list(parent_object[parent_object_key][-1],
                                          parent_object[parent_object_key],
                                          key_index)

    _convert_dict_to_list(unflattened_dict, None, None)
    return unflattened_dict
def unflatten_list ( flat_dict , separator = '_' ) : _unflatten_asserts ( flat_dict , separator ) unflattened_dict = unflatten ( flat_dict , separator ) def _convert_dict_to_list ( object_ , parent_object , parent_object_key ) : if isinstance ( object_ , dict ) : for key in object_ : if isinstance ( object_ [ key ] , dict ) : _convert_dict_to_list ( object_ [ key ] , object_ , key ) try : keys = [ int ( key ) for key in object_ ] keys . sort ( ) except ( ValueError , TypeError ) : keys = [ ] keys_len = len ( keys ) if ( keys_len > 0 and sum ( keys ) == int ( ( ( keys_len - 1 ) * keys_len ) / 2 ) and keys [ 0 ] == 0 and keys [ - 1 ] == keys_len - 1 and check_if_numbers_are_consecutive ( keys ) ) : parent_object [ parent_object_key ] = [ ] for key_index , key in enumerate ( keys ) : parent_object [ parent_object_key ] . append ( object_ [ str ( key ) ] ) _convert_dict_to_list ( parent_object [ parent_object_key ] [ - 1 ] , parent_object [ parent_object_key ] , key_index ) _convert_dict_to_list ( unflattened_dict , None , None ) return unflattened_dict
Unflattens a dictionary, first assuming no lists exist and then tries to
    identify lists and replaces them
    This is probably not very efficient and has not been tested extensively
    Feel free to add test cases or rewrite the logic
    Issues that stand out to me:
    - Sorting all the keys in the dictionary, which specially for the root
    dictionary can be a lot of keys
    - Checking that numbers are consecutive is O(N) in number of keys

    :param flat_dict: dictionary with no hierarchy
    :param separator: a string that separates keys
    :return: a dictionary with hierarchy