0

I have two tuples of possibly different lengths:

t1 = ('abc', 'def', 'ghi', 'jkl')
t2 = ('def', 'jkl')

I need to get a resulting tuple that contains bool, holding True if the value in t1 is in t2 and False otherwise.

I am looking for the following result:

result = (False, True, False, True)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Andi
  • 3,196
  • 2
  • 24
  • 44

2 Answers2

7

Using a generator expression and the tuple constructor, you can iterate over t1 and check if each element is in t2:

result = tuple(element in t2 for element in t1)

If t2 is a large tuple, it may be faster to convert it to a set first.

s2 = set(t2)
result = tuple(element in s2 for element in t1)
Altareos
  • 843
  • 6
  • 13
0

With Python 3.6+, since it maintains insertion order, you can do this using .fromkeys() and .update():

di = dict.fromkeys(t1, False)
di.update(dict.fromkeys(t2, True))
# di = {'abc': False, 'def': True, 'ghi': False, 'jkl': True}
 
>>> tuple(di.values())
(False, True, False, True)

Which can be shortened to:

results=tuple({ **{}.fromkeys(t1, False), **{}.fromkeys(t2, True) }.values())

Or, with Python 3.9+:

result=tuple(({}.fromkeys(t1, False) | {}.fromkeys(t2, True)).values())

If you want to make sure that a value in t2 is not inserted into result as True without a corresponding value in t1, just have a set to determine the overlapping elements:

result=tuple(({}.fromkeys(t1, False) | {}.fromkeys(set(t1)&set(t2), True)).values())

This method is potentially faster for larger sequences since in can be slower for long sequences.

dawg
  • 98,345
  • 23
  • 131
  • 206