I need to use the following syntax to filter the list operations:
a = [ope for ope in operations if ope[0] == 1] The if statement condition is variable and may contain multiple conditions:
a = [ope for ope in operations if ope[0] == 1 and ope[1] == "test"] I use a function to build the condition and return it as a string:
>>>> c = makeCondition(**{"id": 1, "title": 'test'}) >>>> c "ope[0] == 1 and ope[1] == 'test'" Is there a way to integrate the c variable into the list filtering? Something like this (of course, the c variable is evaluated as a string in the below example):
a = [ope for ope in operations if c] Thanks for help!
a = [... if eval(c)]?a = [... if eval(c)]does work, thanks @Chris! Consider adding your comment as an answer so that I can accept it. @tripleee: I don't get what you mean. Could you tell me more with a basic syntax example?conditions[0] = lambda ope : ope[0] == 1and so on. Fill up a list with all the conditions as lambda functions inside, then in the list comprehension, select the one condition you want to use witha = [ope for ope in operations if conditions[0](ope)].evel-- pretty much ever...