Last active
December 10, 2016 11:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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