a dictionary from a list

Peter Hansen peter at engcorp.com
Sat Jun 25 03:29:05 CEST 2005

Dave Cook wrote:
> On 2005-06-24, infidel <saint.infidel at gmail.com> wrote:
>>dict((x, None) for x in alist)
> Whoa, I thought dictionary comprehensions were still planned feature.  I
> guess I gotta start paying closer attention.

Added in Python 2.4, it's actually a generator expression as the sole 
argument to a generic dict() constructor.  Think of the generator 
expression as sort of like the list comprehension that it resembles, 
minus the square brackets, but which doesn't have to create the entire 
list before the dict() constructor starts to consume the elements.


More information about the Python-list mailing list