I have posted a recipe with the itertools solution suggested in this thread: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/212959 Thanks to everybody who gave feedback! Michele