---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- product([1, 2], ['A', 'B'])
returns:
- '[[1, ''A''], [1, ''B''], [2, ''A''], [2, ''B'']]'
dependencies:
- python/_helpers/_itertools
authors:
original by:
- Kevin van Zonneveld (https://kvz.io)
notes: []
parityVerified:
- Python 3.12
isTypescript: true
type: function
layout: function
title: Python's itertools.product in TypeScript
description: >-
Here's what our current TypeScript equivalent to Python's
itertools.product looks like.
function: product
category: itertools
language: python
permalink: python/itertools/product/
alias:
- /functions/python/product/
- /functions/itertools/product/
- /python/product/
---
{% codeblock lang:typescript %}import { itertoolsProduct } from '../_helpers/_itertools.ts'
export function product(...args: unknown[]): unknown[][] {
// discuss at: https://locutus.io/python/itertools/product/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: product([1, 2], ['A', 'B'])
// returns 1: [[1, 'A'], [1, 'B'], [2, 'A'], [2, 'B']]
return itertoolsProduct(args)
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { itertoolsProduct } from '../_helpers/_itertools.ts'
export function product(...args) {
// discuss at: https://locutus.io/python/itertools/product/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: product([1, 2], ['A', 'B'])
// returns 1: [[1, 'A'], [1, 'B'], [2, 'A'], [2, 'B']]
return itertoolsProduct(args)
}{% endcodeblock %}
{% codeblock lang:typescript %}// python/_helpers/_itertools (Locutus helper dependency)
type RepeatOptions = {
repeat?: unknown
}
function toIterableArray(iterable: unknown, functionName: string): unknown[] {
if (Array.isArray(iterable)) {
return [...iterable]
}
if (typeof iterable === 'string') {
return [...iterable]
}
if (isIterable(iterable)) {
return Array.from(iterable)
}
throw new TypeError(`${functionName}() expected an iterable`)
}
function parseProductArgs(args: unknown[]): { iterables: unknown[]; repeat: number } {
if (args.length === 0) {
return { iterables: [], repeat: 1 }
}
const options = extractRepeatOptions(args[args.length - 1])
if (!options) {
return { iterables: args, repeat: 1 }
}
return {
iterables: args.slice(0, -1),
repeat: toNonNegativeInteger(options.repeat ?? 1, 'product'),
}
}
function itertoolsProduct(args: unknown[]): unknown[][] {
const { iterables, repeat } = parseProductArgs(args)
const basePools = iterables.map((iterable) => toIterableArray(iterable, 'product'))
const pools =
repeat === 1
? basePools
: Array.from({ length: repeat }, () => basePools).flatMap((poolSet) => poolSet.map((pool) => [...pool]))
if (pools.length === 0) {
return [[]]
}
if (pools.some((pool) => pool.length === 0)) {
return []
}
const result: unknown[][] = []
buildProduct(pools, 0, [], result)
return result
}
function buildProduct(pools: unknown[][], index: number, current: unknown[], result: unknown[][]): void {
if (index >= pools.length) {
result.push([...current])
return
}
const pool = pools[index] ?? []
for (const value of pool) {
current.push(value)
buildProduct(pools, index + 1, current, result)
current.pop()
}
}
function toNonNegativeInteger(value: unknown, functionName: string): number {
if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value) || value < 0) {
throw new TypeError(`${functionName}() expected a non-negative integer`)
}
return value
}
function isIterable(value: unknown): value is Iterable {
return (
(typeof value === 'object' || typeof value === 'function') &&
value !== null &&
Symbol.iterator in value &&
typeof value[Symbol.iterator] === 'function'
)
}
function extractRepeatOptions(value: unknown): RepeatOptions | null {
if (!isPlainObject(value)) {
return null
}
const keys = Object.keys(value)
if (keys.length === 0 || keys.some((key) => key !== 'repeat')) {
return null
}
return value
}
function isPlainObject(value: unknown): value is { [key: string]: unknown } {
return typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype
}
// python/itertools/product (target function module)
function product(...args: unknown[]): unknown[][] {
// discuss at: https://locutus.io/python/itertools/product/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: product([1, 2], ['A', 'B'])
// returns 1: [[1, 'A'], [1, 'B'], [2, 'A'], [2, 'B']]
return itertoolsProduct(args)
}{% endcodeblock %}
{% codeblock lang:javascript %}// python/_helpers/_itertools (Locutus helper dependency)
function toIterableArray(iterable, functionName) {
if (Array.isArray(iterable)) {
return [...iterable]
}
if (typeof iterable === 'string') {
return [...iterable]
}
if (isIterable(iterable)) {
return Array.from(iterable)
}
throw new TypeError(`${functionName}() expected an iterable`)
}
function parseProductArgs(args) {
if (args.length === 0) {
return { iterables: [], repeat: 1 }
}
const options = extractRepeatOptions(args[args.length - 1])
if (!options) {
return { iterables: args, repeat: 1 }
}
return {
iterables: args.slice(0, -1),
repeat: toNonNegativeInteger(options.repeat ?? 1, 'product'),
}
}
function itertoolsProduct(args) {
const { iterables, repeat } = parseProductArgs(args)
const basePools = iterables.map((iterable) => toIterableArray(iterable, 'product'))
const pools =
repeat === 1
? basePools
: Array.from({ length: repeat }, () => basePools).flatMap((poolSet) => poolSet.map((pool) => [...pool]))
if (pools.length === 0) {
return [[]]
}
if (pools.some((pool) => pool.length === 0)) {
return []
}
const result = []
buildProduct(pools, 0, [], result)
return result
}
function buildProduct(pools, index, current, result) {
if (index >= pools.length) {
result.push([...current])
return
}
const pool = pools[index] ?? []
for (const value of pool) {
current.push(value)
buildProduct(pools, index + 1, current, result)
current.pop()
}
}
function toNonNegativeInteger(value, functionName) {
if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value) || value < 0) {
throw new TypeError(`${functionName}() expected a non-negative integer`)
}
return value
}
function isIterable(value) {
return (
(typeof value === 'object' || typeof value === 'function') &&
value !== null &&
Symbol.iterator in value &&
typeof value[Symbol.iterator] === 'function'
)
}
function extractRepeatOptions(value) {
if (!isPlainObject(value)) {
return null
}
const keys = Object.keys(value)
if (keys.length === 0 || keys.some((key) => key !== 'repeat')) {
return null
}
return value
}
function isPlainObject(value) {
return typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype
}
// python/itertools/product (target function module)
function product(...args) {
// discuss at: https://locutus.io/python/itertools/product/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: product([1, 2], ['A', 'B'])
// returns 1: [[1, 'A'], [1, 'B'], [2, 'A'], [2, 'B']]
return itertoolsProduct(args)
}{% endcodeblock %}