Created
July 31, 2014 16:55
-
-
Save zopieux/52c6e643cc6dcf963902 to your computer and use it in GitHub Desktop.
Lists vs. iterators
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
def do_work(i): | |
# do something very time-consuming here | |
print("computing stuff for i =", i) | |
return i < 2 | |
# Test with lists | |
print( all( [ do_work(i) for i in range(6) ] ) ) | |
# computing stuff for i = 0 | |
# computing stuff for i = 1 | |
# computing stuff for i = 2 | |
# computing stuff for i = 3 | |
# computing stuff for i = 4 | |
# computing stuff for i = 5 | |
# False | |
# Test with iterator | |
print( all( do_work(i) for i in range(6) ) ) | |
# computing stuff for i = 0 | |
# computing stuff for i = 1 | |
# computing stuff for i = 2 | |
# False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment