you can get rid of many useless (discarded) union operations if you prepare your own special dict: just split the values ββor keys, depending on what you are comparing. This suggests, of course, that the dict is less than the number of all combos.
If you need a connection, you need to change this a bit. I think that if you are not more visible, the problem will not be better optimized than this. And it wonβt be much faster just using a different language.
(filtered_combo for filtered_combo in itertools.chain.from_iterable( combo for combo in (itertools.permutations(bag, x) for x in xrange(max_combo_len, 0, -1))) if filtered_combo in special_dict)
source share