info about list comprehension
maxm
maxm at mxm.dk
Mon Oct 14 07:46:00 EDT 2002
Fabrizio wrote:
> Where can I find information about list comprehension (explaination,
> tutorial, etc.) ?
>
> I have tried to browse the Python documentation and the tuttorial, but with
> little results.
It is really very simple, I don't think you will need a tutorial.
A list comprehension is just a short form of an often re-occuring piece
of code.
# Common data
start_list = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
# the usual way to do it
result_list = []
for item in start_list:
if item > 0:
result_list.append(item)
# list comprehension
result_list = [item for item in start_list if item > 0]
regards Max M
More information about the Python-list
mailing list