I have same list of lists
l = [
['one'],
['', 'any'],
['', 'anynay'],
['', 'val'],
['two'],
['', 'dss'],
['tr'],
['', 'ff'],
['', 'mnb']
]
I want get dict group by rows if row[0] not empty
d = {
'one': [['', 'any'], ['', 'anynay'], ['', 'val']],
'two': [['', 'dss']],
'tr': [['', 'ff'], ['', 'mb']]
}
All my attempts is wrong fundamentally
>Solution :
You can simply iterate over the items and add things as they come along. This does however imply that l is strictly in the format described.
d = {}
current = None
for row in l:
if row[0]:
# New section starts
assert len(row) == 1
assert row[0] not in d
# Create new list for that key
d[row[0]] = current = []
else:
# Add item to the current list
current.append(row)