Skip to content

Instantly share code, notes, and snippets.

@bmispelon
Created March 31, 2017 17:19
Show Gist options
  • Select an option

  • Save bmispelon/7c9664f02d4c4d816466f084feebba78 to your computer and use it in GitHub Desktop.

Select an option

Save bmispelon/7c9664f02d4c4d816466f084feebba78 to your computer and use it in GitHub Desktop.
"""
ValueError: not enough values to unpack (expected 3, got 2)
ValueError: too many values to unpack (expected 3)
"""
def unpack(iterable, length):
i = iter(iterable)
ret = [x for x, _ in zip(i, range(length))]
if len(ret) < length:
raise ValueError("not enough values to unpack (expected {}, got {})".format(length, len(ret)))
try:
next(i)
except StopIteration:
return ret
raise ValueError("too many values to unpack (expected {})".format(length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment