# Cartesian product of two lists

Given two lists of characters, output their Cartesian product, i.e. the list of pairings of each letter from the first list with each letter from the second list.

### Example

"123456" and "abcd" give:

[["1","a"],["1","b"],["1","c"],["1","d"],["2","a"],["2","b"],["2","c"],["2","d"],["3","a"],["3","b"],["3","c"],["3","d"],["4","a"],["4","b"],["4","c"],["4","d"],["5","a"],["5","b"],["5","c"],["5","d"],["6","a"],["6","b"],["6","c"],["6","d"]]


### Input

Two lists of characters or strings. The characters used will be alphanumeric a-z, A-Z, 0-9 and a character can occur both multiple times and in both inputs at the same time.

### Output

The Cartesian product of the input lists. That is, a list of each possible ordered pair of a character from the first list and a character from the second list. Each pair is a list or string or similar of two characters, or of two length-one strings. The output's length will be equal to the product of the lengths of the inputs.

The pairs must be listed in order; first listing the first character of the first list with the first of the second list, followed by all the pairings of the first character of the first list. The last pair consists of the last character of the first list together with the last character of the second list.

The output must be a flat list of pairs; not a 2D matrix where pairs are grouped by their first or second element.

### Test cases

inputs               output

"123456", "abcd"     [["1","a"],["1","b"],["1","c"],["1","d"],["2","a"],["2","b"],["2","c"],["2","d"],["3","a"],["3","b"],["3","c"],["3","d"],["4","a"],["4","b"],["4","c"],["4","d"],["5","a"],["5","b"],["5","c"],["5","d"],["6","a"],["6","b"],["6","c"],["6","d"]]
"abc", "123"         [["a","1"],["a","2"],["a","3"],["b","1"],["b","2"],["b","3"],["c","1"],["c","2"],["c","3"]]
"aa", "aba"          [["a","a"],["a","b"],["a","a"],["a","a"],["a","b"],["a","a"]]

• @Adám Changed. I'm having trouble though wording that repeated characters in an input string can and should cause repeated pairs in the output (assuming that's how interpret it).
– xnor
Jun 8, 2017 at 0:25
• @xnor maybe easier if the order of pairs is fixed?
Jun 8, 2017 at 0:28
• Why does the title say "list" yet the body say "list of characters"? Jun 8, 2017 at 7:56
• Just to be sure: is ["1a", "1b", "1c", "2a", "2b", "2c", "3a", "3b", "3c"] a valid output format? Jun 8, 2017 at 16:22
• You tagged this as code-golf therefore shortest answer wins. In the event of a tie, the first answer to reach that score is usually the winner (currently this one). Give it another few days, at least, before accepting an answer, though, if at all. And see here for guidelines on answering your own question. Jun 9, 2017 at 10:52

# R, 29 bytes

function(x,y)outer(x,y,paste)


Try it online!

Note that R matrix are filled by column, so the result is in the order dictated by the spec.

If allowed to have factors for input and output, there is a built-in... but one needs to extract the resulting levels from the factor so in the end it would be more than 29 bytes.

### R, 11 bytes

interaction


Try it online!

# GolfScript, 21 bytes

~\:a;{a{[.;1$]}%\;}%  Try it online! # Google Sheets, 101 Inputs A1, B1. =ArrayFormula(REGEXEXTRACT(Flatten(MID(A1,SEQUENCE(LEN(A1)),1)&MID(B1,SEQUENCE(1,LEN(B1)),1)),"(.)(.)  ## How it Works • Turn string into character array. The first is vertical, the second is horizontal because of how we call SEQUENCE. • Concatenate every permutation of one letter from each string. (Because one is vertical and the other is horizontal. • Flatten into array • Extract first and second character by Regex • I considered delimiting by commas and splitting, but that would have required more work if the first string had a comma (or whatever delimiting character). ## Notes • Flatten is an undocumented function that turns any range into a column array. # Add++, 8 bytes L*,€€+BF  Try it online! ## How it works L*, ; Create an anonymous function that returns the entire stack ; Example inputs: "abc" "123" € ; Over each char: ["1" "abc"] € ; Over each char: ["1" "a"] + ; Concatenate "a1" ; This returns ["a1" "a2" "a3"] ; This returns [[["a1" "a2" "a3"] ["b1" "b2" "b3"] ["c1" "c2" "c3"]]] BF ; Flatten ["a1" "a2" "a3" "b1" "b2" "b3" "c1" "c2" "c3"]  # Vyxal, 1 byte Π  Try it Online! Vyxal's built ins are fun! # Jq 1.5, 29 bytes map(split(""))|[combinations]  Uses combinations builtin. Sample Run $ jq -Mc  'map(split(""))|[combinations]' <<< '["123456","abcd"]'
[["1","a"],["1","b"],["1","c"],["1","d"],["2","a"],["2","b"],["2","c"],["2","d"],["3","a"],["3","b"],["3","c"],["3","d"],["4","a"],["4","b"],["4","c"],["4","d"],["5","a"],["5","b"],["5","c"],["5","d"],["6","a"],["6","b"],["6","c"],["6","d"]]

\$ echo -n 'map(split(""))|[combinations]' | wc -c
29


# Axiom, 55 bytes

f(a,b)==concat[[[a.x,b.y]for y in 1..#b]for x in 1..#a]


some results

(15) -> f("123456","abcd")
(15)
[[1,a], [1,b], [1,c], [1,d], [2,a], [2,b], [2,c], [2,d], [3,a], [3,b],
[3,c], [3,d], [4,a], [4,b], [4,c], [4,d], [5,a], [5,b], [5,c], [5,d],
[6,a], [6,b], [6,c], [6,d]]
Type: List List Character
(16) -> f("abc","123")
(16)  [[a,1],[a,2],[a,3],[b,1],[b,2],[b,3],[c,1],[c,2],[c,3]]
Type: List List Character
(17) -> f("aa","aba")
(17)  [[a,a],[a,b],[a,a],[a,a],[a,b],[a,a]]
Type: List List Character


# APL NARS 8 chars

{,⍺∘.,⍵}


Copy from https://codegolf.stackexchange.com/a/125115/58988 test

f←{,⍺∘.,⍵}
'123456' f 'abcd'
1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d


# Prolog (SWI), 48 bytes

L-R:-findall([A,B],(member(A,L),member(B,L)),R).


Try it online!

# Scala, 24 bytes

for(x<-_;y<-_)yield(x,y)


Try it online!

I love for comprehensions.

# Pyt, 1 byte

×


Try it online!

Gotta love built-ins.

# Thunno, $$\ 2 \log_{256}(96) \approx \$$ 1.65 bytes

Z!


Attempt This Online!

### Thunno, $$\ 2 \log_{256}(96) \approx \$$ 1.65 bytes

zS
`

Attempt This Online!

Built-in solutions.