Fix 'generator' object has no attribute 'next' error on Python 3