Consider
teams = [[], []]
I want to add a people to teams, but only if (1) they for the same company as the others in the team OR (b) if the team is currently empty. The straight-forward way would be:
for team in teams:
if len(team) > 0: # could also use "if bool(team):"
if team[0].company == new_person.company:
team.append(new_person)
break
else:
continue
else:
team.append(new_person)
break
else:
teams.append([])
teams[-1].append(new_person)
To me that seems like many lines to make a simple decision. The complicating factor is the possibility of an empty list, which will give me an error if I try to look at a property of one of its elements.
How can I say if a_list is empty or a_list[0].property == something:
in one line?