newbie-question about a list

Bengt Richter bokr at oz.net
Sat Nov 19 11:01:42 EST 2005


On 19 Nov 2005 07:06:30 -0800, bobueland at yahoo.com wrote:

>I've seen this construct in a script
>
>>>> [x.capitalize() for x in ['a','b', 'c']]
>['A', 'B', 'C']
>
>I tried another myself
>
>>>> [x+1 for x in [1,2,3]]
>[2, 3, 4]
>>>>
>
>Apparently you can do
>[function(x) for x in list]
>
>I tried to find a description of this in "Library Reference" but
>couldn't find it. Could somebody direct me where this type of construct
>is described.
>
It's called a list comprehension. You can find that term in various indices
of the various docs. In a minute I found these. I'm sure there's more if
you google.

http://docs.python.org/
http://docs.python.org/tut/node18.html#l2h-61
http://docs.python.org/ref/lists.html#l2h-345

Regards,
Bengt Richter



More information about the Python-list mailing list