0

For the following example dictionary, is there a builtin method to get all unique combinations?

a = {
  "a": ["a_1", "a_2"],
  "b": ["b_1", "b_2"]
}

output:

[
  ["a_1", "b_1"], 
  ["a_1", "b_2"], 
  ["a_2", "b_1"], 
  ["a_2", "b_2"]
]
Liondancer
  • 14,518
  • 43
  • 138
  • 238
  • 2
    Thats straightforward with `itertools`... does it have to be built-in? – rafaelc Sep 22 '18 at 03:35
  • 3
    May be `list(itertools.product(*a.values()))` as in https://stackoverflow.com/questions/798854/all-combinations-of-a-list-of-lists – niraj Sep 22 '18 at 03:45

1 Answers1

2

I did this with itertools.product()

import itertools
a = {
  "a": ["a_1", "a_2"],
  "b": ["b_1", "b_2"]
}
print(list(itertools.product(*a.values())))

Output:

[('a_1', 'b_1'), ('a_1', 'b_2'), ('a_2', 'b_1'), ('a_2', 'b_2')]
rafaelc
  • 52,436
  • 15
  • 51
  • 78
KC.
  • 2,848
  • 2
  • 11
  • 21