DEV Community

Discussion on: Understanding loops

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

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

def print_letters_from_countries(countries):
    "prints each letter for each country in a list of countries"
    for country in countries: #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)

def print_letters_from_country(country):
    "prints each letter of a single country"
    for letter in country: #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
Collapse
 
jamesbright profile image
James Ononiwu

Thanks for the recommendation

Collapse
 
waylonwalker profile image
Waylon Walker

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

def print_letters_from_countries(countries):
    for i, country in enumerate(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)

def print_letters_from_country(country):
    for i, letter in enumerate(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)
Collapse
 
jamesbright profile image
James Ononiwu

nice update!