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"]),
    ("speed", ["highSpeed", "lowSpeed"]),
    ("fw", ["fw1", "fw2", "fw3"])
])

for combo in productDict(data):
    # highSpeed only works on fw3
    if combo['speed'] == 'highSpeed' and combo['fw'] in ['fw1', 'fw2']:
        continue
    print(combo)

# Output:
# {'voltage': 'Vmax', 'temp': 'hot', 'speed': 'highSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmax', 'temp': 'hot', 'speed': 'lowSpeed', 'fw': 'fw1'}
# {'voltage': 'Vmax', 'temp': 'hot', 'speed': 'lowSpeed', 'fw': 'fw2'}
# {'voltage': 'Vmax', 'temp': 'hot', 'speed': 'lowSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmax', 'temp': 'cold', 'speed': 'highSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmax', 'temp': 'cold', 'speed': 'lowSpeed', 'fw': 'fw1'}
# {'voltage': 'Vmax', 'temp': 'cold', 'speed': 'lowSpeed', 'fw': 'fw2'}
# {'voltage': 'Vmax', 'temp': 'cold', 'speed': 'lowSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmin', 'temp': 'hot', 'speed': 'highSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmin', 'temp': 'hot', 'speed': 'lowSpeed', 'fw': 'fw1'}
# {'voltage': 'Vmin', 'temp': 'hot', 'speed': 'lowSpeed', 'fw': 'fw2'}
# {'voltage': 'Vmin', 'temp': 'hot', 'speed': 'lowSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmin', 'temp': 'cold', 'speed': 'highSpeed', 'fw': 'fw3'}
# {'voltage': 'Vmin', 'temp': 'cold', 'speed': 'lowSpeed', 'fw': 'fw1'}
# {'voltage': 'Vmin', 'temp': 'cold', 'speed': 'lowSpeed', 'fw': 'fw2'}
# {'voltage': 'Vmin', 'temp': 'cold', 'speed': 'lowSpeed', 'fw': 'fw3'}

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.3.tar.gz (4.1 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.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: itertooldict-0.3.3.tar.gz
  • Upload date:
  • Size: 4.1 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.3.tar.gz
Algorithm Hash digest
SHA256 8fc155606dc81a4f4994b42285fce89a8fe841696ba1d2e3b9112d8d26a5d484
MD5 e01009649c07247e04f973a86055f9e8
BLAKE2b-256 e4b3d6cf97c4678d4867c09024663f6bf3701017a2f548cadff57913ccec8c77

See more details on using hashes here.

File details

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

File metadata

  • Download URL: itertooldict-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 3.9 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d6b82530878f1c9e15adfda8ac03af9f6fa223388453a5ca5c04badd7d8fff5f
MD5 9f4f8702e51294d61f759f78e0716b0d
BLAKE2b-256 6ffc43c46412d8832ec0cde2a8205baa1b77b27ab5f0b9e939cc03e558124d31

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