At least in my code, these nested loop thingies are rarely unique. If I have to do it once I probably have to do it many times. If I'm being mindful, I stop repeating myself and just write a generator so that I can invoke it like:
[foo(x) for x in nonempty(bar_mat)]
Mostly this is because I live in fear of ruff's complexity warning, C901.