from itertools import islice
from typing import Iterable, Iterator
def chunked[T](items: Iterable[T], size: int) -> Iterator[list[T]]:
if size < 1:
raise ValueError("size must be >= 1")
it = iter(items)
while batch := list(islice(it, size)):
yield batch
list(chunked(range(7), 3))
# [[0, 1, 2], [3, 4, 5], [6]]
# Bulk-insert in batches of 1,000 — never load the whole file:
import sqlite3
with sqlite3.connect("db.sqlite") as db, open("huge.txt") as f:
for batch in chunked(f, 1000):
db.executemany("INSERT INTO lines VALUES (?)", [(line.strip(),) for line in batch])
Create a free account and build your private vault. Share publicly whenever you want.