Loop in list.

Roy Smith roy at panix.com
Tue Feb 8 10:36:59 EST 2005


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 a list comprehension.  Unfortunately, this is a bad example of
one, since a much simplier way of writing the same thing would be:

mat = ['a'] * 3

After several years of them being in the language, I'm still somewhat
up in the air about the wisdom of list comprehensions.  There is no
doubt that they're convenient and compact, but I think they make code
more difficult to read.  The functional programming folks will
certainly disagree, and I realize I'm probably in the minority on
this, but that's my opinion.



More information about the Python-list mailing list