On the face of it:
if ((var1 in list1 and var2 in list2) or (var1 in list2 and var2 in list2))
to generalize a bit:
mylists = (list1, list2)
if any(var1 in x and var2 in x for x in mylists)
to generalize a bit more:
mylists = (list1, list2)
myvars = (var1, var2)
if any(all(v in x for v in myvars) for x in mylists)
Or you could look at it totally differently:
which_list = dict()
which_list.update((val, 1) for val in list1)
which_list.update((val, 2) for val in list2)
if which_list[var1] == which_list[var2]
of course, that assumes each variable appears in exactly one list.
Note that in all cases, you aren't really checking whether the variable is in the list. A variable cannot be in a list. You're checking whether the current value of the variable is in the list. So if any of the variables refer to identical values, then there's no way to tell from the list which variable name was used to put the value in the list.
As Ashwini Chaudhary says, a set
can generally execute in
faster than a list
or tuple
. But for such small lists it's unlikely to make a noticeable difference.