---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- batched([1, 2, 3, 4, 5], 2)
returns:
- '[[1, 2], [3, 4], [5]]'
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.batched in TypeScript
description: >-
Here's what our current TypeScript equivalent to Python's
itertools.batched looks like.
function: batched
category: itertools
language: python
permalink: python/itertools/batched/
alias:
- /functions/python/batched/
- /functions/itertools/batched/
- /python/batched/
---
{% codeblock lang:typescript %}import { itertoolsBatched } from '../_helpers/_itertools.ts'
export function batched(iterable: unknown, n: unknown): unknown[][] {
// discuss at: https://locutus.io/python/itertools/batched/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: batched([1, 2, 3, 4, 5], 2)
// returns 1: [[1, 2], [3, 4], [5]]
return itertoolsBatched(iterable, n)
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { itertoolsBatched } from '../_helpers/_itertools.ts'
export function batched(iterable, n) {
// discuss at: https://locutus.io/python/itertools/batched/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: batched([1, 2, 3, 4, 5], 2)
// returns 1: [[1, 2], [3, 4], [5]]
return itertoolsBatched(iterable, n)
}{% endcodeblock %}
{% codeblock lang:typescript %}// python/_helpers/_itertools (Locutus helper dependency)
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 itertoolsBatched(iterable: unknown, n: unknown): unknown[][] {
const values = toIterableArray(iterable, 'batched')
const batchSize = toPositiveInteger(n, 'batched', 'n must be at least one')
const result: unknown[][] = []
for (let index = 0; index < values.length; index += batchSize) {
result.push(values.slice(index, index + batchSize))
}
return result
}
function toPositiveInteger(value: unknown, functionName: string, message?: string): number {
const integer = toNonNegativeInteger(value, functionName)
if (integer < 1) {
throw new Error(message ?? `${functionName}() expected a positive integer`)
}
return integer
}
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'
)
}
// python/itertools/batched (target function module)
function batched(iterable: unknown, n: unknown): unknown[][] {
// discuss at: https://locutus.io/python/itertools/batched/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: batched([1, 2, 3, 4, 5], 2)
// returns 1: [[1, 2], [3, 4], [5]]
return itertoolsBatched(iterable, n)
}{% 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 itertoolsBatched(iterable, n) {
const values = toIterableArray(iterable, 'batched')
const batchSize = toPositiveInteger(n, 'batched', 'n must be at least one')
const result = []
for (let index = 0; index < values.length; index += batchSize) {
result.push(values.slice(index, index + batchSize))
}
return result
}
function toPositiveInteger(value, functionName, message) {
const integer = toNonNegativeInteger(value, functionName)
if (integer < 1) {
throw new Error(message ?? `${functionName}() expected a positive integer`)
}
return integer
}
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'
)
}
// python/itertools/batched (target function module)
function batched(iterable, n) {
// discuss at: https://locutus.io/python/itertools/batched/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: batched([1, 2, 3, 4, 5], 2)
// returns 1: [[1, 2], [3, 4], [5]]
return itertoolsBatched(iterable, n)
}{% endcodeblock %}