Dictionary puzzle of the week:
The objective is to write an expression or function that, given a dictionary in which each value is a list, transpose it so the result is list of dictionaries, where:
- The keys of each new dictionary are the same as the keys of the original dictionary
- The values of the
i
th dictionary correspond to thei
th elements of the values of the original dictionary
If that doesn't make sense, here's an example:
# Input:
d = {'a': [4, 5, 6], 'b': [7, 8, 9]}
# Expected Output:
[{'a': 4, 'b': 7}, {'a': 5, 'b': 8}, {'a': 6, 'b': 9}]
For instance, the function might have the following signature:
def transpose_dict(d: Dict[str, List[int]]) -> List[Dict[str, int]]: ...
Edge cases:
- Think about how to handle an empty dict, or values lists of length 0.
- What about if the values lists have different lengths?