Skip to main content

A missing feature of itertools: labeled product of multiple choices from a dictionary.

Project description

itertooldict

A missing feature of itertools: labeled product of multiple choices from a dictionary.

PIP: itertooldict on PyPI

Installation

pip install itertooldict

Usage

productDict takes a dictionary where values are iterables and yields dictionaries representing their Cartesian product.

[!IMPORTANT] collections.OrderedDict is the preferred input type. While standard dictionaries work in modern Python, using OrderedDict ensures that the iteration order and dictionary keys remain consistent across all environments.

from itertooldict import productDict
from collections import OrderedDict

# Preferred usage with OrderedDict
data = OrderedDict([
    ("voltage", ["Vmax", "Vmin"]),
    ("temp", ["hot", "cold"]),
    ("humidity", ["low", "high"])
])

for combo in productDict(data):
    print(combo)

# Output:
# {'voltage': 'Vmax', 'temp': 'hot', 'humidity': 'low'}
# {'voltage': 'Vmax', 'temp': 'hot', 'humidity': 'high'}
# {'voltage': 'Vmax', 'temp': 'cold', 'humidity': 'low'}
# {'voltage': 'Vmax', 'temp': 'cold', 'humidity': 'high'}
# {'voltage': 'Vmin', 'temp': 'hot', 'humidity': 'low'}
# {'voltage': 'Vmin', 'temp': 'hot', 'humidity': 'high'}
# {'voltage': 'Vmin', 'temp': 'cold', 'humidity': 'low'}
# {'voltage': 'Vmin', 'temp': 'cold', 'humidity': 'high'}

Specifying Key Order

You can use the keyorder argument to specify the order of keys in the resulting dictionaries and the order in which the product is calculated.

data = {"a": [1, 2], "b": ["x", "y"]}
# Iterate with 'b' as the outer loop and 'a' as the inner loop
for combo in productDict(data, keyorder=["b", "a"]):
    print(f"b={combo['b']}, a={combo['a']}")
# b=x, a=1
# b=x, a=2
# b=y, a=1
# b=y, a=2

Compatibility with list() and enumerate()

productDict works seamlessly with standard Python functions:

# Convert to list
all_combos = list(productDict(data))

# Use with enumerate
for i, combo in enumerate(productDict(data)):
    print(f"{i}: {combo}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

itertooldict-0.3.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

itertooldict-0.3.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file itertooldict-0.3.2.tar.gz.

File metadata

  • Download URL: itertooldict-0.3.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for itertooldict-0.3.2.tar.gz
Algorithm Hash digest
SHA256 47fcc4bb991f951497fda32c93988ac32a4aac7f6dd1b613ed2851da2d89071f
MD5 b9b7f984459e430c197732efcf0cc0a1
BLAKE2b-256 576cfdf48c8807552bc9a9e4f3a3f2b06a610b8d8855f1f30f5b7dad2fdf579a

See more details on using hashes here.

File details

Details for the file itertooldict-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: itertooldict-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for itertooldict-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4e62356acdd989dfdecf3320ab9eb24f2e429c6ebc9663901a3634b6f8ebee46
MD5 f37dcf96a4adb8015254722cddd15643
BLAKE2b-256 36fde2f72b1d56e4d96421cedc1101c8bd3dec86a4dd9554995788aa30c50ced

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page