What is the best way to create multiple list from a dictionary? My dictionary looks like this:
mydict = {'aaa': ['111', '222', '333'], 'bbb': ['444', '555']}
I'm trying to make lists like this:
aaa ['111', '222', '333']
bbb ['444', '555']
...
I've tried
result = []
For key, value in mydict.item()
result[key].append(value)
This returns a type error.
for key, val in dct.items(): print(key, val)
result[key].append(...)
is does not make sense --result
is a list,result[key]
won't exist (unlesskey
is an integer andresult
contains at leastkey+1
elements, andresult[key]
isn't a list becauseresult
was initialized as an empty list.]
? And the "I am trying to make lists like this" does not make sense becauseaaa ['111', '222', '333']
is not a list. The latter part of that line is a list, butaaa
isn't part of that list. Are you asking how to create a list namedaaa
that contains those three strings, and another namedbbb
that contains those two strings? If so, why? Creating variables with dynamic names is rightly frowned upon.mydict[key]
when you need the list. E.g.another_function(mydict['aaa'])