👋 Hey there, I am Waylon Walker
I am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha
I always advise against nesting loops at all cost. In real projects it quickly gets out of control and you end up 5+ tab stops out. I recommend treating items differently than their containers. Below I have refactored your first example using this methodology, keeping most of your code exactly the same.
# Un-nested for loops
defprint_letters_from_countries(countries):"prints each letter for each country in a list of countries"forcountryincountries:#iterate over countries list
print('beginning of outer loop')print('{} is at index {} of {}'.format(country,countries.index(country),countries),'\n')print('beginning of inner loop for {}'.format(country))print_letters_from_country(country)defprint_letters_from_country(country):"prints each letter of a single country"forletterincountry:#iterate over each country in countries list
print('{} is at index {} of {}'.format(letter,country.index(letter),country))print('end of inner loop for {}'.format(country),'\n')countries=['Germany','USA','Spain']print_letters_from_countries(countries)
Output
beginning of outer loop
Germany is at index 0 of ['Germany', 'USA', 'Spain']
beginning of inner loop for Germany
G is at index 0 of Germany
e is at index 1 of Germany
r is at index 2 of Germany
m is at index 3 of Germany
a is at index 4 of Germany
n is at index 5 of Germany
y is at index 6 of Germany
end of inner loop for Germany
beginning of outer loop
USA is at index 1 of ['Germany', 'USA', 'Spain']
beginning of inner loop for USA
U is at index 0 of USA
S is at index 1 of USA
A is at index 2 of USA
end of inner loop for USA
beginning of outer loop
Spain is at index 2 of ['Germany', 'USA', 'Spain']
beginning of inner loop for Spain
S is at index 0 of Spain
p is at index 1 of Spain
a is at index 2 of Spain
i is at index 3 of Spain
n is at index 4 of Spain
end of inner loop for Spain
👋 Hey there, I am Waylon Walker
I am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha
You can also get the index in a more pythonic fashion by using the enumerate function to get the index while looping. This version, uses enumerate and f-strings.
# Un-nested for loops
defprint_letters_from_countries(countries):fori,countryinenumerate(countries):#iterate over countries list
print('beginning of outer loop')print(f'{country} is at index {i} of {countries}\n')print(f'beginning of inner loop for {country}')print_letters_from_country(country)defprint_letters_from_country(country):fori,letterinenumerate(country):#iterate over each country in countries list
print(f'{letter} is at index {i} of {country}')print(f'end of inner loop for {country}\n')countries=['Germany','USA','Spain']print_letters_from_countries(countries)
I always advise against nesting loops at all cost. In real projects it quickly gets out of control and you end up 5+ tab stops out. I recommend treating items differently than their containers. Below I have refactored your first example using this methodology, keeping most of your code exactly the same.
Output
Thanks for the recommendation
You can also get the index in a more pythonic fashion by using the enumerate function to get the index while looping. This version, uses enumerate and f-strings.
nice update!