Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Last active April 9, 2022 07:30
Show Gist options
  • Save JosephTLyons/ae56cb96fc51775ae7f24016d1e0b911 to your computer and use it in GitHub Desktop.
Save JosephTLyons/ae56cb96fc51775ae7f24016d1e0b911 to your computer and use it in GitHub Desktop.
Gentlemen's addition to spicy loops
# 1: A true classic
# --------------------------------------
animals = ["dog", "cat", "bird", "mouse", "goat"]
for animal in animals:
print(animal)
# 2: A dash of pepper ...
# --------------------------------------
animals = ["dog", "cat", "bird", "mouse", "goat"]
for index, animal in enumerate(animals):
print(animals[index])
# 3: It's a Friday night and there is no dress code
# --------------------------------------
animals = ["dog", "cat", "bird", "mouse", "goat"]\
index = 0
while True:
try:
print(animals[index])
index += 1
except IndexError:
break
# 4: Peak performance
# --------------------------------------
animals = ["dog", "cat", "bird", "mouse", "goat"]
while True:
try:
animal, *animals = animals
print(animal)
except ValueError:
break
# 5: Bring the thunder
# --------------------------------------
animals = ["dog", "cat", "bird", "mouse", "goat"]
funcs = [next for _ in animals]
animal_iter = iter(animals)
for func in funcs:
print(func(animal_iter))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment