I was wondering if there was an easy solution to the the following problem. The problem here is that I want to keep every element occurring inside this list after the initial condition is true. The condition here being that I want to remove everything before the condition that a value is greater than 18 is true, but keep everything after. Example
Input:
p = [4,9,10,4,20,13,29,3,39]
Expected output:
p = [20,13,29,3,39]
I know that you can filter over the entire list through
[x for x in p if x>18]
But I want to stop this operation once the first value above 18 is found, and then include the rest of the values regardless if they satisfy the condition or not. It seems like an easy problem but I haven’t found the solution to it yet.
>Solution :
You could use enumerate and list slicing inside next :
out = next((p[i:] for i, item in enumerate(p) if item > 18), [])
Output:
[20, 13, 29, 3, 39]