I'd like to combine two lists. If I have the following two lists: {a,b,c,d}
and {1,2,3,4}
what do I need to do so that I get {{a,1}, {b,2}, {c,3}, {d,4}}
?

- 11,964
- 12
- 50
- 96
7 Answers
An esoteric method is Flatten
, which (from the Help Section on Flatten) also allows Transpose of a 'ragged' array.
Flatten[ {{a, b, c, d}, {1, 2, 3, 4, 5}}, {{2}, {1}}]
Out[6]= {{a, 1}, {b, 2}, {c, 3}, {d, 4}, {5}}

- 4,214
- 2
- 35
- 42
-
@rcollyer yes, I wrote my own function to do that, only to later find out it was right there all along. – Mr.Wizard Mar 21 '11 at 14:40
-
4`Thread[List[{a, b, c, d}, {1, 2, 3, 4}]]` hasn't been mentioned – 681234 Mar 21 '11 at 16:36
-
I'm keeping my answer with the `transpose` option, because that is exactly what I wanted in my case. This however is a more general solution, which clearly earns a +1 – Martijn Mar 21 '11 at 20:01
-
1+1 Same here. Never knew this was added to `Flatten`. Is it me or is the description (`Flatten[list,{{s11,s12,...},{s21,s22,...},...}] flattens list by combining all levels sij to make each level i in the result.`) pretty much incomprehensible? It took me several minutes of intense staring before comprehension started to seep in. – Sjoerd C. de Vries Mar 21 '11 at 22:25
-
@TomD `Partition[Riffle[{a, b, c, d}, {1, 2, 3, 4}],2]` hasn't been mentioned either ;-) – Sjoerd C. de Vries Mar 21 '11 at 22:27
-
@Sjoerd don't forget `Tr[Table[{i, j}, {i, Symbol /@ CharacterRange @@ Reverse@Characters["da"]}, {j, Array[Binomial, {#, #}][[;; #, 1]] &[4]}], {##} &, 2]` :D – Mr.Wizard Mar 22 '11 at 11:04
-
@Mr.Wizard *Chuckle* This is sheer genius. I bow to you and will refer to you in my mind as King Jobsecurity I. – Sjoerd C. de Vries Mar 22 '11 at 11:50
-
@Sjoerd You like that? I wonder if a Mathematica obfuscated code contest would fall outside the domain of StackExchange. I expect to be revealed as an amateur if we have one. – Mr.Wizard Mar 22 '11 at 12:18
One possible solution is
MapThread[List,{{a,b,c,d},{1,2,3,4}}]

- 38,639
- 9
- 64
- 83
-
2If one is about to do something with the pairs that are created, this (`MapThread`) is often more efficient than `Apply` after `Transpose`. – Mr.Wizard Mar 21 '11 at 14:42
If you have lists with the columns of a matrix:
l = Table[Subscript[g, Sequence[j, i]], {i, 5}, {j, 5}]
Transpose will give you the rows:
Transpose@l // MatrixForm

- 60,527
- 15
- 115
- 190
listA={a,b,c,d};
listB=[1,2,3,4};
table=Transpose@{# & @@@ listA, # & @@@ listB}

- 226
- 2
- 14
This is a great question. I had become stuck thinking there was a default way to do this with Table, but not so. The answers below are fairly intuitive, and can be easily generalized to other similar situations.
l1 = {a,b,c,d};
l2 = {1,2,3,4};
pairs = Table[{l1[[i]], l2[[i]]}, {i, 1, Length[l1]}]
MapThread does this sort of thing also. This is less elegant than Howard's MapThread solution, but also more readable in some sense. Look at MapThread docs. The function is defined inline (pure function):
pairs = MapThread[{#1, #2} &, {l1, l2}]

- 277
- 2
- 12
In case a, b, c, d themselves are also list, use the following:
MapThread[Flatten[{#1[[All]],#2}]&,{l1,l2}]//TableForm

- 505
- 5
- 14