Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Created March 18, 2014 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PuercoPop/9613366 to your computer and use it in GitHub Desktop.
Save PuercoPop/9613366 to your computer and use it in GitHub Desktop.
for item in collection:
do_stuff(item)
#becomes:
iterator = iter(collection)
while True:
try:
item = iterator.next()
# The code from the for indent block goes here. (No lambdas :/)
do_stuff(item)
except StopIteration:
# The code from the else clause goes here.
break
# Don't trust me try this:
collection = iter([1, 2, 3, 4, 5]) # Python lists are iterable, but not
# iterators
while True:
try:
item = collection.next()
print "{0} bottles of beer on the wall".format(item)
except StopIteration:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment