Skip to content

Instantly share code, notes, and snippets.

@zopieux
Created July 31, 2014 16:55
Show Gist options
  • Save zopieux/52c6e643cc6dcf963902 to your computer and use it in GitHub Desktop.
Save zopieux/52c6e643cc6dcf963902 to your computer and use it in GitHub Desktop.
Lists vs. iterators
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