Skip to content

Instantly share code, notes, and snippets.

@plavjanik
Created June 16, 2022 06:31
Show Gist options
  • Save plavjanik/e9a1f3a222aa9c2c77f21aa05ed3d819 to your computer and use it in GitHub Desktop.
Save plavjanik/e9a1f3a222aa9c2c77f21aa05ed3d819 to your computer and use it in GitHub Desktop.
Looping over two collections
names = ["Pierre", "Per", "Peter"]
colors = ["cyan", "magenta", "yellow", "black"]
# ❌
n = min(len(names), len(colors))
for i in range(n):
print(f"{names[i]} --> {colors[i]}")
# ✔️
for name, color in zip(names, colors):
print(f"{name} --> {color}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment