Process tuple contents on the fly

Tim Chase python.list at tim.thechases.com
Mon Apr 15 15:10:42 EDT 2013


On 2013-04-15 12:05, Barrett Lewis wrote:
> >   d = {}
> >   for key, d[key] in (("this",18), ("that",17), ("other",38)):
> >     print key
> >     do_something(d)
> 
> Why not use a dict comprehension?
> d = {k:v for k,v in  (("this",18), ("that",17), ("other",38))}
> 
> I feel this is more straightforward and easier to read. the results
> are the same however.

In the particular case I did it in, I needed the incremental results
passed to a function, not just the final result.  I don't think this
made it into the final code, rather it was expanded to be more
readable.  But the discovery made me feel a disturbance in the
Pythonic force of the universe. :*)

-tkc





More information about the Python-list mailing list