Enumerate Every Settings Grid from Option Lists
Arrays & HashingAuto-graded · 18 test casesLast updated
Build the Cartesian product of named option lists into a list of dictionaries, handling empty inputs and a defined output order.
Build a Full Settings Grid
You are given a dictionary that maps each setting name to a list of candidate values for that setting. Write a function that returns every possible full assignment of settings. Each assignment is a dictionary that picks exactly one candidate value for every name in the input.
Output order
The result must be in this exact order:
- Process the names in the order they appear in the input dictionary.
- The first name changes slowest and the last name changes fastest, as in nested loops where the first name is the outermost loop.
- Within one name, candidate values are used in the order they appear in its list.
Each returned dictionary should list its keys in the same order as the input dictionary.
Special cases
- If the input dictionary is empty, return a list containing a single empty dictionary:
[{}]. - If any name has an empty list of candidates, no full assignment exists, so return
[]. - Candidate values can be of any type (numbers, strings,
None, and so on). Do not inspect or modify them. - Do not modify the input dictionary. Every returned dictionary must be a separate object.
Constraints
- The input has at most 10 names.
- Each candidate list has at most 5 values.
- Candidate lists contain no repeated values.
Examples
Example 1
Input: build_search_grid({'learning_rate': [0.01, 0.1], 'batch_size': [32, 64]})
Output: [{'learning_rate': 0.01, 'batch_size': 32}, {'learning_rate': 0.01, 'batch_size': 64}, {'learning_rate': 0.1, 'batch_size': 32}, {'learning_rate': 0.1, 'batch_size': 64}]
Example 2
Input: build_search_grid({'optimizer': ['sgd', 'adam'], 'dropout': [0, 0.2], 'epochs': [10, 20]})
Output: [{'optimizer': 'sgd', 'dropout': 0, 'epochs': 10}, {'optimizer': 'sgd', 'dropout': 0, 'epochs': 20}, {'optimizer': 'sgd', 'dropout': 0.2, 'epochs': 10}, {'optimizer': 'sgd', 'dropout': 0.2, 'epochs': 20}, {'optimizer': 'adam', 'dropout': 0, 'epochs': 10}, {'optimizer': 'adam', 'dropout': 0, 'epochs': 20}, {'optimizer': 'adam', 'dropout': 0.2, 'epochs': 10}, {'optimizer': 'adam', 'dropout': 0.2, 'epochs': 20}]
Example 3
Input: build_search_grid({})
Output: [{}]
Hints
Hint 1
Each full assignment chooses one value from every list. This is the Cartesian product of the lists.
Hint 2
Fix an order for the keys, such as the dictionary's own order. Then either recurse over key positions, or use itertools.product on the value lists and zip each tuple back with the keys.
Hint 3
You do not need special handling for the empty cases. The product of zero lists yields one empty tuple, and a product containing an empty list yields nothing. If you use recursion, append a copy of the partial dictionary, not the shared one.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a Cartesian product. Take the keys in their given order and collect their value lists. Each full assignment picks one element from each list, and the required output order (first key slowest, last key fastest) is the order itertools.product produces. For each tuple from product, zip it with the keys to build a new dictionary.
A recursive alternative walks the keys by position. At position i it tries each value for key i and recurses to i + 1. When all keys are assigned, it appends a copy of the current dictionary. Looping values in list order gives the same ordering.
Why it is correct
- Completeness and uniqueness: each combination corresponds to exactly one choice of index per list, and
productenumerates each such choice exactly once. - Validity: every tuple has one element per list, so each dictionary has exactly one value per key.
- Empty input: the product of zero lists is a single empty tuple, so the result is
[{}]. - An empty candidate list: a product that includes an empty set has no elements, so the result is
[]. - Order:
productvaries the rightmost list fastest, which matches the required order.
Complexity
Let n be the number of keys and P the product of the list lengths.
- Time: O(P * n), since we build
Pdictionaries of sizen. - Space: O(P * n) for the output, plus O(n) of auxiliary space.
Reference solution
from itertools import product
from typing import Dict, List
def build_search_grid(options: Dict[str, List]) -> List[Dict]:
names = list(options)
candidate_lists = [options[name] for name in names]
return [dict(zip(names, combo)) for combo in product(*candidate_lists)]