Why isn't this unpacking work for my dictionary?
The dictionary is build as follows:
cutDict = {'scene1': [('scene3', 1001, 1125)]}
I'm trying to unpack this like:
for key, (cut, fIn, fOut) in cutDict.iteritems():
print key
print cut
print fIn
print fOut
but this results in:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
Right now I'm building the dictionary via:
cutDict.setdefault(itm, [])
for cutItm in itm.cut_items_ordered:
cutInOut = (mostcutItm, recentcutItm.cut_item_in, callcutItm.cut_item_out)
cutDict[itm].append(cutInOut):
Your value is not a tuple, it is a list with one element, which is a tuple; the nested tuple has 3 elements. Because the outer container has only one element, your unpacking assignment fails.
The following does work, by adding another layer, provided there is always exactly one element:
for key, ((cut, fIn, fOUT),) in cutDict.iteritems():
or you can use list syntax:
for key, [(cut, fIn, fOUT)] in cutDict.iteritems():
Demo:
>>> cutDict = {'scene1': [('scene3', 1001, 1125)]}
>>> for key, ((cut, fIn, fOUT),) in cutDict.iteritems():
... print key, cut, fIn, fOut
...
scene1 scene3 1001 1125
>>> for key, [(cut, fIn, fOUT)] in cutDict.iteritems():
... print key, cut, fIn, fOut
...
scene1 scene3 1001 1125
Alternatively, remove the nesting, the outer list is redundant.
If your dictionary really can have more than one item in it, then the outer list is of course not redundant, but then you can't use iterable unpacking either. You'd have to add another loop instead:
for key, cuts in cutDict.iteritems():
for cut, fIn, fOUT in cuts:
# ...