Skip to content

Instantly share code, notes, and snippets.

@HintikkaKimmo
Last active December 10, 2016 11:46
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 HintikkaKimmo/55b124e508ed2693634b9b08501ff557 to your computer and use it in GitHub Desktop.
Save HintikkaKimmo/55b124e508ed2693634b9b08501ff557 to your computer and use it in GitHub Desktop.
Using enumerate to walk through the list of cities printing cities and their locations
cities = ['Dublin', 'Amsterdam', 'Helsinki', 'London']
# Bad way to to walk trough the list print them and their location on the list
i = 0
for city in cities:
print(i, city)
i += 1
# Good way is to use enumerate function.
# Enumerate documentation at: https://docs.python.org/3/library/functions.html#enumerate
for i, city in enumerate(cities):
print(i, city)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment