Can python create a dictionary from a list comprehension?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon May 28 03:25:17 EDT 2007


In <1180299814.129770.93220 at o11g2000prd.googlegroups.com>, half.italian
wrote:

> [entries.__setitem__(int(d.date.strftime('%m'))], d.id) for d in
> links]
> 
> btw...I was curious of this too.  I used 'dir(dict)' and looked for a
> method that might do what we wanted and bingo!

This is really ugly.  Except `__init__()` it's always a code smell if you
call a "magic" method directly instead of using the corresponding
syntactic sugar or built in function.  And using a list comprehension just
for side effects is misleading because the reader expects a (useful) list
to be build when stumbling over a list comp and it's wasteful because an
unnecessary list of `None`\s is build and thrown away for no reason other
than to have a one liner.  This is not Perl!  ;-)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list