How can I make a list of dictionaries according to the Cartesian product of values in a source dictionary (“explode” the dictionary)?

I think you want the Cartesian product, not a permutation, in which case itertools.product can help: >>> from itertools import product >>> d = {‘Color’: [‘Red’, ‘Yellow’], ‘Size’: [‘Small’, ‘Medium’, ‘Large’]} >>> [dict(zip(d, v)) for v in product(*d.values())] [{‘Color’: ‘Red’, ‘Size’: ‘Small’}, {‘Color’: ‘Red’, ‘Size’: ‘Medium’}, {‘Color’: ‘Red’, ‘Size’: ‘Large’}, {‘Color’: ‘Yellow’, ‘Size’: ‘Small’}, {‘Color’: … Read more

Cartesian product of streams in Java 8 as stream (using streams only)

Passing the streams in your example is never better than passing Lists: private static <T> Stream<T> cartesian(BinaryOperator<T> aggregator, List<T>… lists) { … } And use it like this: Stream<String> result = cartesian( (a, b) -> a + b, Arrays.asList(“A”, “B”), Arrays.asList(“K”, “L”), Arrays.asList(“X”, “Y”) ); In both cases you create an implicit array from varargs … Read more

String Replacement Combinations

How about: from itertools import product def filler(word, from_char, to_char): options = [(c,) if c != from_char else (from_char, to_char) for c in word] return (”.join(o) for o in product(*options)) which gives >>> filler(“1xxx1”, “x”, “5”) <generator object <genexpr> at 0x8fa798c> >>> list(filler(“1xxx1”, “x”, “5”)) [‘1xxx1’, ‘1xx51’, ‘1x5x1’, ‘1×551′, ’15xx1′, ’15×51’, ‘155×1’, ‘15551’] (Note that … Read more

Generate all possible combinations for Columns(cross join or Cartesian product)

in post-pandemic new world we can solve this with: =INDEX(FLATTEN(A2:A3&” “&TRANSPOSE(B2:B4))) to account for future expansion we can do: =INDEX(FLATTEN(FILTER(A2:A; A2:A<>””)&” “&TRANSPOSE(FILTER(B2:B; B2:B<>””)))) for 3 columns: =INDEX(FLATTEN(FLATTEN( FILTER(A2:A; A2:A<>””)&” “&TRANSPOSE( FILTER(B2:B; B2:B<>””)))&” “&TRANSPOSE( FILTER(C2:C; C2:C<>””)))) 4 columns: =INDEX(FLATTEN(FLATTEN(FLATTEN( FILTER(A2:A; A2:A<>””)&” “&TRANSPOSE( FILTER(B2:B; B2:B<>””)))&” “&TRANSPOSE( FILTER(C2:C; C2:C<>””)))&” “&TRANSPOSE( FILTER(D2:D; D2:D<>””)))) for more see: https://stackoverflow.com/a/74160711/5632629