def variations(iterable, optional=lambda x: False):
<EOL>iterable = tuple(iterable)<EOL>o = [optional(x) for x in iterable]<EOL>a = set()<EOL>for p in product([False, True], repeat=sum(o)):<EOL><INDENT>p = list(p)<EOL>v = [b and (b and p.pop(<NUM_LIT:0>)) for b in o]<EOL>v = tuple(iterable[i] for i in xrange(len(v)) if not v[i])<EOL>a.add(v)<EOL><DEDENT>return sorted(a, cmp=lambda x, y: len(y) - len(x))<EOL>
Returns all possible variations of a sequence with optional items.