# What can I use as an equivalent of python zip in mathematica inside of Sum[]? [closed]

I want to take two lists of the same length: widths and weights, and sum a function (of 3 variables) over a single index using the elements from both lists that have that index. Then I want to plot the function.

If I only sum over one of the lists, I can use

scatterfunc[x1_, x2_, y1_] =
Sum[y1/(4*x1) + (1 - y1)/(4*x2) -
y1/(gamma + x1) - (1 - y1)/(gamma + x2), {gamma, widths}];
Manipulate[
ContourPlot[scatterfunc[x1, x2, y1], {x1, 0, 1}, {x2, 0, 1},
ColorFunction -> "GrayTones", PlotLegends -> Automatic], {y1, 0, 1}]


But I've been unable to find the equivalent of python's zip(widths,weights) in order to sum over a single index using both lists. I want something like

scatterfunc[x1_, x2_, y1_] =
Sum[y1/(4*x1) + (1 - y1)/(4*x2) -
alpha*y1/(gamma + x1) - (1 - y1)/(gamma + x2), {{gamma,alpha}, zip[widths,weights]}];
Manipulate[
ContourPlot[scatterfunc[x1, x2, y1], {x1, 0, 1}, {x2, 0, 1},
ColorFunction -> "GrayTones", PlotLegends -> Automatic], {y1, 0, 1}]


Thanks.

## closed as off-topic by Jens, m_goldberg, MarcoB, Bob Hanlon, RunnyKineJul 30 '15 at 2:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Jens, m_goldberg, MarcoB, Bob Hanlon, RunnyKine
If this question can be reworded to fit the rules in the help center, please edit the question.

• zip() is effectively the same as Transpose[], but your problem looks as if it will be better served by MapThread[] + Total[]. – J. M. is away Jul 29 '15 at 16:46
• You can do something like Sum[...ww[[1]]...ww[[2]],{ww,Transpose@{widths,weights}} ]. Where ww[[1]] is width, and ww[[2]] is weight. – N.J.Evans Jul 29 '15 at 16:47
• You don't even need Sum at all because Plus and Times automatically thread over lists. Start by reading http://reference.wolfram.com/language/howto/CombineAndRearrangeLists.html – Jens Jul 29 '15 at 16:49
• @N.J.Evans, this worked perfectly. I had tried transpose, but without '@'. I'm not super familiar with mathematica. What does '@' do? – chia Jul 29 '15 at 17:02
• @chia, f @ x is just the same as f[x]; a prefix form of function application to a single argument. – J. M. is away Jul 29 '15 at 17:06

In python, the zip function does this:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]


You can do this in the Wolfram Language, for example, with MapThread:

x = {1,2,3};
y = {4,5,6};
MapThread[ List, {x, y} ]  (* gives {{1,4}, {2,5}, {3,6}} *)

• Isn't Transpose[] more "traditional"? – J. M. is away Jul 29 '15 at 16:47
• Also, if one wants the full generality of zip(), Flatten[] with a properly set second argument might be more appropriate. – J. M. is away Jul 29 '15 at 16:56

Thanks @N.J.Evans, this solved my problem.

scatterfunc[x1_, x2_, y1_] =
Sum[y1/(4*x1) + (1 - y1)/(4*x2) -
ww[[2]]*y1/(ww[[1]] + x1) - (1 - y1)/(ww[[1]] + x2), {ww,
Transpose@{widths, weights}}];
Manipulate[
ContourPlot[scatterfunc[x1, x2, y1], {x1, 0, 1}, {x2, 0, 1},
ColorFunction -> "GrayTones", PlotLegends -> Automatic], {y1, 0, 1}]

• Consider Total[y1/(4*x1) + (1 - y1)/(4*x2) - weights y1/(widths + x1) - (1 - y1)/(widths + x2)] to exploit listability. – J. M. is away Jul 29 '15 at 17:00