Given a list L
in Python, and two elements x
and y
that are guaranteed to be in L
, I wish to know whether x
appears before y
in L
.
If there is only one pair, I can simply loop over L
until I find either x
or y
. This takes time linear in the length of L
. But, I have to answer many such queries for the same L
(for different x
and y
). Is there an efficient way, maybe a data structure or iterator in Python, that I can use to answer many queries on the same L
quickly?