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.
Installation
pip install itertooldict
Usage
itertooldict takes a dictionary where values are iterables and yields dictionaries representing their Cartesian product.
from itertooldict import itertooldict
data = {
"voltage": ["Vmax", "Vmin"],
"temp": ["hot", "cold"]
}
for combo in itertooldict(data):
print(combo)
# Output:
# {'voltage': 'Vmax', 'temp': 'hot'}
# {'voltage': 'Vmax', 'temp': 'cold'}
# {'voltage': 'Vmin', 'temp': 'hot'}
# {'voltage': 'Vmin', 'temp': 'cold'}
Excluding Combinations
You can use the .remove() method to exclude specific combinations or patterns.
it = itertooldict(data)
it.remove({"voltage": "Vmax", "temp": "hot"})
for combo in it:
print(combo)
# {'voltage': 'Vmax', 'temp': 'cold'}
# {'voltage': 'Vmin', 'temp': 'hot'}
# {'voltage': 'Vmin', 'temp': 'cold'}
You can also remove based on partial matches:
Randomizing Order
You can randomize the order of the generated combinations:
it = itertooldict(data).random()
for combo in it:
print(combo) # Order will be shuffled
Updating Key Order
You can specify the order of keys in the resulting dictionaries:
it = itertooldict(data)
it.updateKeyOrder(["temp", "voltage"])
for combo in it:
print(combo) # {'temp': 'hot', 'voltage': 'Vmax'}, ...
Compatibility with list() and enumerate()
itertooldict works seamlessly with standard Python functions:
# Convert to list
all_combos = list(itertooldict(data))
# Use with enumerate
for i, combo in enumerate(itertooldict(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file itertooldict-0.1.2.tar.gz.
File metadata
- Download URL: itertooldict-0.1.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19622c3dab64b2ea767291137de1692577a6f1ecb7273e9b54d98b67b28bd5d9
|
|
| MD5 |
beb3a8254d501fd4037eeede5a166a50
|
|
| BLAKE2b-256 |
050f0e7fffb72648213243c43f50b90daa00345f036821353545ece5099efb7e
|
File details
Details for the file itertooldict-0.1.2-py3-none-any.whl.
File metadata
- Download URL: itertooldict-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9e0b2f090489a6ad5d84ca9df8a23b1d39036fb1dc7b384a6e92db4444e39a6
|
|
| MD5 |
e375b79e3b90e2cf5a79926ebc591607
|
|
| BLAKE2b-256 |
e41a284fd510688cf892921591617d27cd59d6fccc3b917ef8375379bc7b89ba
|