Get the previous value based on a specific element
I am trying to use a script where we use a list item and then get the previous item.
lst = [1, 3, 4, 6, 8, 10]
next_item = next((x for x in lst if x > 6), None)
print next_item #8
This code works correctly for the element after 6. However, I need the element before 6.
I look in the docs for the prev method but I can't find anything related. Any ideas about this?
+3
source to share