Loop in list.

Simon Brunning simon.brunning at gmail.com
Tue Feb 8 10:00:45 EST 2005


On Tue, 08 Feb 2005 06:50:31 -0800 (PST), Jim <jtp11 at att.net> wrote:
> Where did this type of structure come from:
> 
> mat = ['a' for i in range(3)]    ?
> 
> This will produce a list of three elements but
> I don't see reference for it in any of the books.

It's called a "List Comprehension". There's a good historical reason
for the name, but you can be excused in not having looked under that
name in the index. See
<http://www.amk.ca/python/2.0/index.html#SECTION000600000000000000000>
for details.

They *should* be mentioned in most books, provided that it's vaguely
recent and covers Python 2.0 or later.

List comps have a cool new little sister, generator expressions - see
<http://www.brunningonline.net/simon/blog/archives/001025.html>.

-- 
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list