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):
    voltage = combo["voltage"]
    temp = combo["temp"]
    humidity = combo["humidity"]
    print(f"Testing {voltage} at {temp} with {humidity} humidity")

# Output:
# Testing Vmax at hot with low humidity
# Testing Vmax at hot with high humidity
# Testing Vmax at cold with low humidity
# Testing Vmax at cold with high humidity
# Testing Vmin at hot with low humidity
# Testing Vmin at hot with high humidity
# Testing Vmin at cold with low humidity
# Testing Vmin at cold with high humidity

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.1.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.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: itertooldict-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a1ea7641d653ec3c900388506c8014d11226e6dd6fee465c84198c2829924abf
MD5 46fa63e2a8eeeb4151c734919ea45397
BLAKE2b-256 d696f05ec9b357ccaf2f6956dde5d7c35175ea06fb3745fd5e0bae862b8e06cf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: itertooldict-0.3.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 73f5ec1faee9a716123338418fffef971abbb4725a102e53f0c7629bc52470ab
MD5 34dc8b287cb5f695adf2d4789ade0af4
BLAKE2b-256 28d42b8f2598cfc7b31bfd7831f20e756670d21887e6e1694bc5cc3ac304df6c

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