Nested for Loop
We will use a nested loop to print each city multiple times in a structured format, resembling a triangle pattern.
12345678travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Outer loop for controlling rows for i in range(1, len(travel_list) + 1): # Inner loop for controlling columns for j in range(i): print(travel_list[j], end=' ') # Print cities in a row print('') # Move to the next line after each row
- Outer loop:
the
forloop withrange(1, len(travel_list) + 1)determines the number of rows. Each iteration represents a row.icontrols how many cities are printed in the current row; - Inner loop:
the
forloop withrange(i)iterates over the cities to be printed in the current row.jaccesses the city names in thetravel_listup to the current row index; - Printing:
the
print(travel_list[j], end=' ')ensures cities are printed on the same row.print('')moves to the next line after each row.
Using a nested loop, you can iterate through a nested list and interact with each of its elements.
12345678910111213# Define a nested list containing sublists with words starting with 'A' and 'T' nested_list = [ ["Apple", "Avocado", "Apricot"], ["Tomato", "Tangerine", "Tea"], ["Almond", "Thyme", "Tuna"] ] # Iterate through each sublist in the nested list for sublist in nested_list: # Iterate through each item in the current sublist for item in sublist: # Convert the item to lowercase and print it print(item.lower(), end=' ')
Swipe to start coding
You are working on a navigation system that processes lists of trips, where each trip includes multiple countries. However, a data processing error has caused all country names to appear in lowercase, making them unreadable by the system.
To fix this issue, you need to extract and format the country names correctly.
- Extract all country names from
tripsand store them incountries. - Capitalize each country name before adding it to
countriesusingcapitalize()method.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the output of the first code sample looks?
What would happen if I changed the order of the loops?
Can you show an example of using nested loops with different types of loops?
Awesome!
Completion rate improved to 5
Nested for Loop
Swipe to show menu
We will use a nested loop to print each city multiple times in a structured format, resembling a triangle pattern.
12345678travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Outer loop for controlling rows for i in range(1, len(travel_list) + 1): # Inner loop for controlling columns for j in range(i): print(travel_list[j], end=' ') # Print cities in a row print('') # Move to the next line after each row
- Outer loop:
the
forloop withrange(1, len(travel_list) + 1)determines the number of rows. Each iteration represents a row.icontrols how many cities are printed in the current row; - Inner loop:
the
forloop withrange(i)iterates over the cities to be printed in the current row.jaccesses the city names in thetravel_listup to the current row index; - Printing:
the
print(travel_list[j], end=' ')ensures cities are printed on the same row.print('')moves to the next line after each row.
Using a nested loop, you can iterate through a nested list and interact with each of its elements.
12345678910111213# Define a nested list containing sublists with words starting with 'A' and 'T' nested_list = [ ["Apple", "Avocado", "Apricot"], ["Tomato", "Tangerine", "Tea"], ["Almond", "Thyme", "Tuna"] ] # Iterate through each sublist in the nested list for sublist in nested_list: # Iterate through each item in the current sublist for item in sublist: # Convert the item to lowercase and print it print(item.lower(), end=' ')
Swipe to start coding
You are working on a navigation system that processes lists of trips, where each trip includes multiple countries. However, a data processing error has caused all country names to appear in lowercase, making them unreadable by the system.
To fix this issue, you need to extract and format the country names correctly.
- Extract all country names from
tripsand store them incountries. - Capitalize each country name before adding it to
countriesusingcapitalize()method.
Solution
Thanks for your feedback!
single