Python Language Verify only one element in iterable


Example

Use 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