17

I'm trying to use the following code on a list of lists to create a new list of lists, whose new elements are a certain combination of elements from the lists inside the old list...if that makes any sense! Here is the code:

 for index, item in outputList1:
    outputList2 = outputList2.append(item[6:].extend(outputList1[index+1][6:]))

However, I get a "Too many values to unpack" error. I seem to even get the error with the following code:

    for index, item in outputList1:
       pass

What could I be doing wrong?

MattyZ
  • 1,541
  • 4
  • 24
  • 41
  • 3
    'enumerate' is missing, i.e., for index, item in enumerate(otputList1) : – doug Aug 02 '10 at 08:25
  • Also my syntax in the first line is incorrect, I just need: outputList2.append(item[6:].extend(outputList1[index+1][6:])) Been working with C too long! – MattyZ Aug 02 '10 at 08:48
  • In Python3 you can do `for index, *item in outputList1`. Then `index` has the first element and `item` has the rest. – Martin Thoma Dec 28 '13 at 02:44
  • possible duplicate of ['too many values to unpack', iterating over a dict. key=>string, value=>list](http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list) – Pablo Jomer Jul 30 '14 at 08:57
  • @Bitrex i know i'm super late with this, but it would be super helpful to future visitors of this page for you to specify what `outputList1` contains. you say it's a "list of lists," but how many elements are in each inner list? it'd be great if you could write a line `outputList1 = [ . . .]` at the top. – abcd Apr 23 '15 at 17:12

2 Answers2

27

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.

Carson Myers
  • 37,678
  • 39
  • 126
  • 176
  • That is not how iteration over a dict works. Try it in a terminal and you'll get blah is not iterable, where blah is the class of the key. What you are thinking of is for key,value in dict.items(): but that is not default behaviour as you've said. – Andrew Aug 02 '10 at 09:05
  • I get the same error trying to iterate through a list which contains a tuple: `lst=[('a','b')] for key, value in lst:` Could it be because of BOM unicode sign in my first tuple? – Hrvoje T Mar 09 '17 at 07:25
  • @HrvojeT what version of Python? It works fine for me - if there was a BOM in one of the strings then it shouldn't affect the iterability of the tuple – Carson Myers Mar 09 '17 at 13:21
  • @CarsonMyers I got a solution here http://stackoverflow.com/questions/42695716/unpacking-iterables-in-python3/42695799#42695799 Thnaks anyway – Hrvoje T Mar 09 '17 at 13:25
11

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

for index,item in enumerate(outputList1) :
  pass
Andrew
  • 2,943
  • 18
  • 23