1

The following fails in Python 3.5:

for key, (a, b) in {'my_key': ('foo', 'bar')}:
  print(key, a, b)

with:

ValueError: too many values to unpack (expected 2)

Why is it unable to unpack the tuple properly?

Josh
  • 11,979
  • 17
  • 60
  • 96

1 Answers1

2

If you use the items() method on the dictionary, it will work.

>>> for key, (a,b) in {'my_key': ('foo','bar')}.items():
...     print(key, a, b)
...
my_key foo bar

See: Iterating over dictionaries using 'for' loops

Community
  • 1
  • 1
arewm
  • 649
  • 3
  • 12