# Mathematica's equivalent to the Python's Zip command in List-comprehension? [closed]

What is Mathematica's equivalent to Python's ZIP command? Is this called nesting? It means iterating over sub-sets of values like the below example. How can I do it in Mathematica?

Example in Python that I want in Mathematica, I want the ZIP!

>>> [(ii,jj) for (ii,jj) in zip((1,10,100),(2,20,200))]
[(1, 2), (10, 20), (100, 200)]
>>> [(ii,jj,kk) for (ii,jj,kk) in zip((1,10,100),(2,20,200),(3,30,300))]
[(1, 2, 3), (10, 20, 30), (100, 200, 300)]


[Update] question got solved, Python's Zip is Transpose in Mathematica. The real challenge is now how to operate it like Spherical-Cartesian conversion below

**Mathematica Example: input as spherical (L,Azimuth,Phi) and output as cartesian (x,y,z), how?**

L = {874, 4513, 1487};
A = {120 Degree, 140 Degree, 180 Degree};
Inc = {14.1 Degree, 66.2 Degree, 66.2 Degree};
tuple = Transpose[{L, A, Inc}];
hhh = Print[N[{#1*Cos[#2], #1*Sin[#2]*Cos[#3], #1*Sin[#2]*Sin[#3]}]] &[r, \[Theta], \[Phi]];
hhh @@ tuple

• Check Map and Transpose. Or maybe Thread does what you want? In[135]:= Thread[f[{1, 10, 100}, {2, 20, 200}, {3, 30, 300}]] Out[135]= {f[1, 2, 3], f[10, 20, 30], f[100, 200, 300]} Commented Sep 22, 2014 at 22:46
• Why did you Thread[Times[...]] instead of threading List? Commented Sep 22, 2014 at 23:11
• As noted above, Thread is the equivalent to zip. MapThread provides control similar to list comprehension. Commented Sep 22, 2014 at 23:20
• MapThread[Function[{x,y,x},x+y+z],{xlist,ylist,zlist}] is pretty darn close to [x+y+z fir x,y,z in zip(xlist,ylist,zlist)] Commented Sep 23, 2014 at 0:25
• @hhh it isn't working because you do not have the correct syntax for [Function, which is Function[{args},CoordinateTransform[(* etc *)]][args]. Note the [args] at the end. I'd suggest using the pure function form if you are going to put it in a MapThread. You do not need to be a "super guru", you just need to pay attention to the documented syntax. Commented Sep 23, 2014 at 2:22

The function you want is MapThread - no Transpose needed.

MapThread[f[#1, #2, #3] &, {L, A, Inc}]

(* {f[874, 120 \[Degree], 0.246091], f[4513, 140 \[Degree], 1.15541],
f[1487, 180 \[Degree], 1.15541]} *)


And for your function:

MapThread[N[{#1*Cos[#2], #1*Sin[#2]*Cos[#3], #1*Sin[#2]*Sin[#3]}] &,
{L, A, Inc}]
(* {{-437., 734.102, 184.394}, {-3457.16, 1170.64, 2654.21}, {-1487., 0., 0.}} *)


EDIT: And for your other function - note the use of SlotSequence:

SpherCat = CoordinateTransform["Spherical" -> "Cartesian", {##}] &

(* CoordinateTransform["Spherical" -> "Cartesian", {##1}] & *)

MapThread[SpherCat, {L, A, Inc}]

(* CoordinateTransform::bdpt: Evaluation point {1487,180 \[Degree],1.15541} is
incompatible with the coordinate assumptions of the specified coordinate chart. >> *)

{{734.102, 184.394, -437}, {1170.64,
2654.21, -4513 Cos[40 \[Degree]]},
CoordinateTransform[
"Spherical" -> "Cartesian", {1487, 180 \[Degree], 1.15541}]} *)


There seems to be an error in your input (perhaps you intended the reverse transformation?) but the question of how to simply and elegantly operate on the lists you want is dealt with.

• Can you elaborate this with another function such as the ready CoordinateTransform where you don't specify the & explicitly? How to use MapThread with it? I am trying to do it with it now to replace the self-done function here. I must say that I am so confused to Mathematica, why on earth they cannot get the shxt out and design the syntax clean (sorry just frustrated in trying to understand a simple thing like tuple manipulation).
– hhh
Commented Sep 23, 2014 at 1:38
• I did it in two different ways. I got one method working but MapThread not working i.sstatic.net/ySiIZ.png, trying...
– hhh
Commented Sep 23, 2014 at 1:56
• @hhh, as mentioned in my other comment, that's nothing to do with MapThread and everything to do with incorrect syntax for the full form of Function. Also kindly stop moving the goal posts by revising the question and function of interest. No wonder this question has been downvoted. Commented Sep 23, 2014 at 2:23
• I like the fact that you are never giving up +1, real hxcker attitude! Sometimes it is muddy but always fighting to the goal. Thank you.
– hhh
Commented Sep 23, 2014 at 3:18

You could get the lists you want simply by entering them as a nested list and transposing it:

In[60]:= Transpose@{{1, 10, 100}, {2, 20, 200}}

Out[60]= {{1, 2}, {10, 20}, {100, 200}}


and

In[61]:= Transpose@{{1, 10, 100}, {2, 20, 200}, {3, 30, 300}}

Out[61]= {{1, 2, 3}, {10, 20, 30}, {100, 200, 300}}

• Actually, you are right in this +1 -- took me a long time and Freenode gurus to hit me a little! Transpose is the equivalent to Python's Zip -- dxmn I got a new question here. The real challenge is how to operate the tuples in Mathematica by the Spherical-Cartesian function. Somehow with ## and &...ideas?
– hhh
Commented Sep 22, 2014 at 23:52
• zip[{} ..] := {}; zip [p : xs__] /; MemberQ[{p}, {}] := {}; zip[p : Repeated[_List, {2, \[Infinity]}]] := Join[Through[{Apply[First[{#}]&, {##}, {1}]&}[p]], zip[(Rest[{##1}] & @@@ {p}) /. {x__List} :> Sequence[x]]] one can use this implementation as well for sub-lists of same or different lengths Commented Aug 18, 2017 at 22:52

Here's an answer to the question you have now erased.

questions = {"name", "quest", "favorite color"};
answers = {"lancelot", "the holy grail", "blue"};
"What is your " <> questions[[#]] <> "? It is " <> answers[[#]] <> "." & /@ Range[3]


{"What is your name? It is lancelot.", "What is your quest? It is the holy grail.", "What is your favorite color? It is blue."}

• Err I provided an easier example to make question easier to understand, could you update? I felt this example was not as clear as the latter one. There must be something as succint as in Python built in to Mathematica, always trying to find the most elegant solution.
– hhh
Commented Sep 22, 2014 at 22:38

God in Freenode helped to find some material related to this one, important to become more self-aware of things! Tuple ordering is not trivial thing. Starting the succicnt summary here!

Summary

• Pair lists to create tuples in order covering ragged tuple ordering, Python's zip can be implemented with many different methods such as Flatten for ragged case and Transpose for non-ragged case