Last active
June 11, 2021 12:54
-
-
Save rajmayank/912f51dfbf1bd829a093234796cd12d9 to your computer and use it in GitHub Desktop.
Python-Iterators-Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_list = ["item #1", "item #2", "item #3", "item #4"] | |
my_list_iter = iter(my_list) | |
print(type(my_list)) # <class 'list'> | |
print(my_list) # ['item #1', 'item #2', 'item #3', 'item #4'] | |
print(type(my_list_iter)) # <class 'list_iterator'> | |
print(my_list_iter) # <list_iterator object at 0x10aa87d60> | |
print(next(my_list_iter)) # item #1 | |
print(next(my_list_iter)) # item #2 | |
print(next(my_list_iter)) # item #3 | |
print(next(my_list_iter)) # item #4 | |
print(next(my_list_iter)) # Exception: StopIteration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment