iterator? way of generating all possible combinations?

Scott David Daniels scott.daniels at
Tue May 30 20:36:53 CEST 2006

akameswaran at wrote:
> However, none of the algo's I have checked will work with generated
> sequences, or iterable classes, as posited in my first post.
> While appropriate to the current domain, ie dice.  What if you want
> combinations of extrememely large lists, say 3 sets of 10 mil items.
> In such a case, I really do want my sets to be generators rather than
> lists or set objects.
> This is what had me stumped before, and still has me stumped.

     class Counter(object):
         def __init__(self, digits, iterable=None):
             self.digits = digits
             self.iterable = iterable
         def __iter__(self):
             for digit in self.digits:
                 single = digit,
                 if self.iterable is None:
                     yield single
                     for rest in self.iterable:
                         yield single + rest

     for v in Counter('ab', Counter('cd', Counter('ef', Counter('gh')))):
	print v

This works with "iterables" (and produces), rather than "iterators",
which is vital to the operation.

--Scott David Daniels
scott.daniels at

More information about the Python-list mailing list