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"])
])

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

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

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(combo)
# {'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.0.tar.gz (3.9 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.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: itertooldict-0.3.0.tar.gz
  • Upload date:
  • Size: 3.9 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.0.tar.gz
Algorithm Hash digest
SHA256 d515e74942c1eaf0d8128c7b20ee9edb2d7c0180bba0a6504b2eb3e22701e933
MD5 208cb8d16fe2ca0d34dcbd022b229f1d
BLAKE2b-256 634d6c9a87b35b8a1d82150e7235c935e07d9791854d5653f8dc403d3b083a98

See more details on using hashes here.

File details

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

File metadata

  • Download URL: itertooldict-0.3.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b2ae0abb2f842f9f34372ebca51a5072959eca3dec9ed57206598c091f4be3e
MD5 092600f125d1eecccae20412cc437c01
BLAKE2b-256 c2a9518bb125c9afbb34478d24affaae8cfc1d96b328e8a31580378f5a12507b

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