Instead of `list(x)[0]` use `next(x)`. Catch `StopIteration` and throw a `ValueError`