Filter clauses in list comprehension,
As one particularly useful extension, the for loop nested in a comprehension expression can have an associated if clause to filter out of the result items for which the test is not true.
//This will return the list of all the even numbers in range 100
print [index for index in range(100) if 0 == index%2]
But I am looking at possibility of adding a function that can be called for evaluating the condition.? With this feature I will be able to add more complex conditions in it.
Something like this,
def is_even(x):
return 0 is x%2
print [index +100 for index in range(10) 0 is_even(index)]
Aucun commentaire:
Enregistrer un commentaire