## Prompt
I'm writing a Python program. I have data in the following structure.
```json
{
"A": [
10,
11
],
"B": [
20
],
"C": [
30,
31
],
"D": [
40
],
"E": [
50,
51
]
}
```
Some keys have a single value, while others have multiple values. I'd like to generate every possible combination of values, selecting one value from each key at a time. In this example, there are 8 possible combinations.
```
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 51]
[10, 20, 31, 40, 50]
[10, 20, 31, 40, 51]
[11, 20, 30, 40, 50]
[11, 20, 30, 40, 51]
[11, 20, 31, 40, 50]
[11, 20, 31, 40, 51]
```
I feel like the answer should be simple with itertools, but my attempts so far have failed.
```python
>>> import itertools
>>> data = {'A': [10, 11], 'B': [20], 'C': [30, 31], 'D': [40], 'E': [50, 51]}
>>> print(list(itertools.product(data.values())))
[([10, 11],), ([20],), ([30, 31],), ([40],), ([50, 51],)]
>>> print(list(itertools.combinations(data.values(), len(data))))
[([10, 11], [20], [30, 31], [40], [50, 51])]
```
How can I generate these combinations in Python?
## ChatGPT response
To generate all possible combinations of values, selecting one value from each key at a time, you can use the `itertools.product()` function. However, before using `itertools.product()`, you need to extract the values from the dictionary and pass them as separate arguments. Here's an example of how you can achieve this:
```python
import itertools
data = {
"A": [10, 11],
"B": [20],
"C": [30, 31],
"D": [40],
"E": [50, 51]
}
values = data.values() # Extract the values from the dictionary
combinations = list(itertools.product(*values))
for combination in combinations:
print(list(combination))
```
Output:
```
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 51]
[10, 20, 31, 40, 50]
[10, 20, 31, 40, 51]
[11, 20, 30, 40, 50]
[11, 20, 30, 40, 51]
[11, 20, 31, 40, 50]
[11, 20, 31, 40, 51]
```
In the code above, `itertools.product(*values)` is used to unpack the values and pass them as separate arguments to the `product()` function. This way, each value list is treated as an individual iterable, and `product()` generates all the combinations you desire.