<sxh python> #!/usr/bin/env python # printing 2 combinations def print2Combs(n): for i in xrange(0, n): for j in xrange(i+1, n): print (i,j) print2Combs(5) print # returning next 2 combination each time class CombIter: def __init__(self, n): self.n = n def next(self): self.j = self.j + 1 if (self.j==self.n): self.i = self.i + 1 self.j = self.i + 1 if (self.i==self.n-1): raise StopIteration return (self.i, self.j) def __iter__(self): self.i = 0 self.j = 0 return self for p in CombIter(5): print p print # same as above but using coroutines def gen2Combs(n): for i in xrange(0, n): for j in xrange(i+1, n): yield (i,j) for p in gen2Combs(5): print p print </sxh>