The problem
I would like to create a dictionary of dicts out of a flat list I have in order to add a "sequentiality" piece of information, but I am having some trouble finding a solution.
The list is something like
a = ['Q=123', 'W=456', 'E=789', 'Q=753', 'W=159', 'E=888']
and I am shooting for a dict
like:
dictionary = {
'Step_1': {
'Q=123',
'W=456',
'E=789'
},
'Step_2': {
'Q=753',
'W=159',
'E=888'
}
}
I would like to end up with a function with an arbitrary number of Steps
, in order to apply it to my dataset. Suppose that in the dataset there are lists like a
with 1 <= n <6 Steps
each.
My idea
Up to now, I came up with this:
nsteps = a.count("Q")
data = {}
for i in range(nsteps):
stepi = {}
for element in a:
new = element.split("=")
if new[0] not in stepi:
stepi[new[0]] = new[1]
else:
pass
data[f"Step_{i}"] = stepi
but it doesn't work as intended: both steps in the final dictionary contain the data of Step_1
.
Any idea on how to solve this?