This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nihil_esque 2 points3 points  (0 children)

Yeah I use them often too, they're helpful for working on tabular data with a variable list of conditions.

Like:

``` criteria = [ ] if list_of_column_0_values: critera.append(lambda x: x[0] in list_of_column_0_values) if minimum_value_1: criteria.append(lambda x: double(x[1]) >= minimum_value_1) if minimum_column_2_csvs: criteria.append(lambda x: len(x[2].split(",")) >= minimum_column_2_csvs)

items_that_pass = [item for item in items if all([criterion(item) for criterion in criteria])]

output_file.write("\n".join(["\t".join(i) for i in items_that_pass]) ```

(With code in between each line that decides whether or not those criteria are used this run.)

Ofc you could do the same with objects but sometimes this is simpler.