List of lists and "Too many values to unpack"

You've forgotten to use enumerate, you mean to do this:

for index,item in enumerate(outputList1) :
  pass

the for statement iterates over an iterable -- in the case of a list, it iterates over the contents, one by one, so in each iteration, one value is available.

When using for index, item in list: you are trying to unpack one value into two variables. This would work with for key, value in dict.items(): which iterates over the dicts keys/values in arbitrary order. Since you seem to want a numerical index, there exists a function enumerate() which gets the value of an iterable, as well as an index for it:

for index, item in enumerate(outputList1):
    pass

edit: since the title of your question mentions 'list of lists', I should point out that, when iterating over a list, unpacking into more than one variable will work if each list item is itself an iterable. For example:

list = [ ['a', 'b'], ['c', 'd'] ]
for item1, item2 in list:
    print item1, item2

This will output:

a b
c d

as expected. This works in a similar way that dicts do, only you can have two, three, or however many items in the contained lists.

Tags:

Python