strange for loop construct

Sardaukary at gmail.com Sardaukary at gmail.com
Fri Jan 5 21:39:08 CET 2007


I was googling for an example of the classic word frequency program in
Python as I'm just learning the language, and wanted to see how other
people implemented it.

I found this blog post
http://digitalhistory.uwo.ca/dhh/index.php/2006/08/20/easy-pieces-in-python-word-frequencies/
(with a much more concise version than I managed) but I can't seem to
find any mention in various Python documentation of the following
construct

wordfreq = [wordlist.count(p) for p in wordlist]

I would expect

for p in wordlist:
    wordfreq.append(wordlist.count(p))


I didn't know you could have an expression in the same line.




More information about the Python-list mailing list