May-26-2020, 06:32 AM
I would perhaps use bisect for this, if the tuple is sorted
>>> import bisect >>> t = ('bar', 'baz', 'foo') >>> w = 'bazaaa' >>> w.startswith(t) True >>> index = bisect.bisect_right(t, w) - 1 >>> t[index] 'baz'