Find partial tuple in list of tuple

As @tobias_k suggested, the simplest way is to convert tuples and subtuples to string, and use the in operator for substring check:

l = [('a','b','c','d','e'),
          ('z', 'aa', 'ab', 'ac', 'ad'),

def subtuple_index(tuples, t):
    def tuple2str(t): return ',{},'.format(','.join(t))
    t = tuple2str(t)
    for i,x in enumerate(map(tuple2str, tuples)):
        if t in x: return i
    return -1

>>> subtuple_index(l, ('m','n'))
>>> subtuple_index(l, ('z','aa','ab'))
>>> subtuple_index(l, ('m','o'))
>>> subtuple_index(l, ('z','a'))

Leave a Comment