def tupleslices(list, size): '''Generates a list of tuples of a specific size. For example: tupleslices([1,2,3,4], 2) returns [(1,2), (2,3), (3,4)] ''' result = [] for i in range(len(list) - size + 1): t = tuple(list[i:(i + size)]) result.append(t) return result if __name__ == '__main__': print "Unit testing" assert tupleslices(range(5), 2) == [(0,1), (1,2), (2,3), (3,4)] assert tupleslices(range(5), 4) == [(0,1,2,3), (1,2,3,4)]