Comment

Paulo Almeida

I didn't mean to imply the story wasn't relevant, on the contrary, since what I said only applies to f4. It's a bonus of that solution that you could forgo the for loop entirely. I wasn't even thinking about speed, just readability. You get something you can iterate over as a list, with values_list and flat=True, but even if you wanted an actual list, you could just call list() on it, with no need for the loop.

That said, I wasn't aware of StringAgg, James's solution is indeed awesome.

Parent comment

Peter Bengtsson

See James's comment below. There are even faster ways but if you really really need the list of values into python, to do something the database can't do, then this story is relevant.