Interesting list() un-optimization

Steven D'Aprano steve+comp.lang.python at
Fri Mar 8 18:59:22 CET 2013

On Wed, 06 Mar 2013 22:20:11 -0500, Roy Smith wrote:

> I stumbled upon an interesting bit of trivia concerning lists and list
> comprehensions today.
> We use mongoengine as a database model layer.  A mongoengine query
> returns an iterable object called a QuerySet.  The "obvious" way to
> create a list of the query results would be:
>     my_objects = list(my_query_set)
> and, indeed, that works.  But, then I found this code:
>    my_objects = [obj for obj in my_query_set]
> which seemed a bit silly.  I called over the guy who wrote it and asked
> him why he didn't just write it using list().  I was astounded when it
> turned out there's a good reason!

And why was that not documented in the code?

I hope you took this fellow out and gave him a damned good thrashing!


More information about the Python-list mailing list