Use itertools to avoid unnecessary array and list creation