Verify only one element in iterable
suggest changeUse unpacking to extract the first element and ensure it’s the only one:
a, = iterable
def foo():
yield 1
a, = foo() # a = 1
nums = [1, 2, 3]
a, = nums # ValueError: too many values to unpack (expected 1, got 3)
An empty iterable raises ValueError: not enough values to unpack (expected 1, got 0).
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents