newbie-question about a list

B

bobueland

B

bobueland

This type of construct seems to be called "list comprehension".
Googling for

Python "list comprehension"

gives a lot of hints that describe the construct.
 
B

Bengt Richter

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
 
M

Mike Meyer

I've seen this construct in a script
[x.capitalize() for x in ['a','b', 'c']]
['A', 'B', 'C']
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.

As others have pointed out, it's called a "list comprehension". But
it's syntax, so the proper place to look for it is the "Language
Reference", not the "Library Reference". Except in this case, the
language reference is fairly opaque, so you're better off using the
google references others have already provided.

You might also look for the related construct, "generator
expressions".

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top