[Solved] Convert flat list to dictionary with keys at regular intervals [closed]


Here’s a straightforward solution with a simple loop:

dict_x = {}
for value in list_x:
    if isinstance(value, str):
        dict_x[value] = current_list = []
    else:
        current_list.append(value)

Basically, if the value is a string then a new empty list is added to the dict, and if it’s a list, it’s appended to the previous list.

solved Convert flat list to dictionary with keys at regular intervals [closed]