def variations(iterable, optional=(lambda x: False)):
 iterable = tuple(iterable)
 o = [optional(x) for x in iterable]
 a = set()
 for p in product([False, True], repeat=sum(o)):
  p = list(p)
  v = [(b and (b and p.pop(0))) for b in o]
  v = tuple((iterable[i] for i in range(len(v)) if (not v[i])))
  a.add(v)
 return sorted(a, cmp=(lambda x, y: (len(y) - len(x))))

def variations ( iterable , optional = ( lambda x : False ) ) : iterable = tuple ( iterable ) o = [ optional ( x ) for x in iterable ] a = set ( ) for p in product ( [ False , True ] , repeat = sum ( o ) ) : p = list ( p ) v = [ ( b and ( b and p . pop ( 0 ) ) ) for b in o ] v = tuple ( ( iterable [ i ] for i in range ( len ( v ) ) if ( not v [ i ] ) ) ) a . add ( v ) return sorted ( a , cmp = ( lambda x , y : ( len ( y ) - len ( x ) ) ) )
returns a generator of the n-sized variations of seq .