Nesting loops inside of each other in python makes for much harder code to understand, it takes more brain power to understand, and is thus more error prone than if its avoidable. One issue with this complexity is that toy examples may make sense, but most real example will grow and become more deeply nested over time. Avoiding this complexity from the start can help simplify the project in the future.
Lets take a pretty simple example where we are using a ficticious library to get some sales data for our transportation company. The api allows us to fetch teh sales data for one class of vehicle and one region at a time.
import pandas as pd from datastore import get_sales # ficticious library cars = ['sedan', 'coupe', 'hatchback'] regions = ['US', 'CA', 'MX']
❌ Nesting Loops #
We have setup to fetch our data with two lists that...
...