assert len(row1) == len(row2)
for col in range(len(row1)):
    if row1[col] != row2[col]:
        return col, len(row2)
assert False, 'should not have duplicates'
prev_row = None
for row in rows:
    level = get_level_diff(prev_row, row)
for l in range(*level):
    print(2 * l * ' ', row[l])
prev_row = row
Присоединяйтесь к нескольким таблицам в SQLAlchemy/Flask