filter a list of strings

Peter Pearson pkpearson at nowhere.invalid
Sat Dec 5 14:42:04 EST 2015


On Thu, 3 Dec 2015 10:27:19 +0100, <c.buhtz at posteo.jp> wrote:
[snip]
> I often saw constructions like this
>   x for x in y if ...
> But I don't understand that combination of the Python keywords (for,
> in, if) I allready know. It is to complex to imagine what there really
> happen.

Don't give up!  List comprehensions are one of the coolest things
in Python.  Maybe this simple example will make it click for you:

>>> [x**2 for x in [1,2,3,4] if x != 2]
[1, 9, 16]

-- 
To email me, substitute nowhere->runbox, invalid->com.



More information about the Python-list mailing list