strange for loop construct

Gabriel Genellina gagsl-py at
Fri Jan 5 21:49:48 CET 2007

At Friday 5/1/2007 17:39, Sardaukary at wrote:

>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.

That's known as a "list comprehension" and is roughly equivalent to 
your code. Section 5 of the tutorial covers them.

Gabriel Genellina
Softlab SRL 


Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 

More information about the Python-list mailing list