filter a list of strings

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Dec 3 04:46:54 EST 2015


On 03.12.2015 10:27, c.buhtz at posteo.jp wrote:
 >
 > 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.
 >
 > I understand this
 >    for x in y:
 >      if ...
 >
 > But what is about the 'x' in front of all that?
 >

The leading x states which value you want to put in the new list. This 
may seem obvious in the simple case, but quite often its not the 
original x-ses found in y that you want to store, but some 
transformation of it, e.g.:

[x**2 for x in y]

is equivalent to:

squares = []
for x in y:
     squares.append(x**2)





More information about the Python-list mailing list