dict comprehension question.

Roy Smith roy at panix.com
Sat Dec 29 20:58:46 CET 2012


In article 
<724d4fea-606a-4503-b538-87442f6bcebc at ci3g2000vbb.googlegroups.com>,
 Quint Rankid <qbr567 at gmail.com> wrote:

> Newbie question.  I've googled a little and haven't found the answer.
> 
> Given a list like:
> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]
> I would like to be able to do the following as a dict comprehension.
> a = {}
> for x in w:
>     a[x] = a.get(x,0) + 1

Why are you trying to do this mind-blowing thing?  Other than as an 
entry in an obfuscated code contest, what is this for?

Anyway, I don't think this is possible with a dict comprehension.  
Entries in the dict depend on entries previously put into the dict.  I 
don't see any way a dict comprehension can deal with this.



More information about the Python-list mailing list