Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created May 28, 2020 21:35
Show Gist options
  • Save ZhouYang1993/f5d63fd3d60a831a1a45daf335604daf to your computer and use it in GitHub Desktop.
Save ZhouYang1993/f5d63fd3d60a831a1a45daf335604daf to your computer and use it in GitHub Desktop.
Iterators in Python
# convert an Iterable to an Iterator
my_list_iterator = iter(my_list)
while True:
try:
# get the next item
element = next(my_list_iterator)
except StopIteration:
# if StopIteration is raised, stop for loop
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment