use itertools following jgraham's suggestion