Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created May 28, 2020 16:28
Show Gist options
  • Save ZhouYang1993/00425994628744547c37edf0c060f94e to your computer and use it in GitHub Desktop.
Save ZhouYang1993/00425994628744547c37edf0c060f94e to your computer and use it in GitHub Desktop.
Iterators in Python
my_list = [1, 2, 3, 4, 5]
print(my_list)
# [1, 2, 3, 4, 5]
my_list_iterator = iter(my_list)
print(my_list_iterator)
# <list_iterator object at 0x7f048a4b67f0>
next(my_list_iterator)
# 1
next(my_list_iterator)
# 2
next(my_list_iterator)
# 3
next(my_list)
# TypeError: 'list' object is not an iterator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment