list comprehension

Simon Forman rogue_pedro at yahoo.com
Thu Jun 29 13:34:53 EDT 2006


a wrote:
> can someone tell me how to use them
> thanks

basically, a list comprehension is just like a for loop,  if you wrote
it out the "long way" it would be something like this:

results = []
for var in some_iterable:
    if some condition:
        results.append(some expression)


The list comprehension version:

results = [some expression for var in some_iterable if some condition]


There's more to it, but that's the basic idea.

Hope this helps,
~Simon




More information about the Python-list mailing list