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
  • 15,721
  • 51
  • 149
  • 255
  • 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
  • 57,686
  • 15
  • 58
  • 82
KC.
  • 2,981
  • 2
  • 12
  • 22