19

I have read x-data (from text files) into list1, and y-data similarly into list2:

list1 = { 0.0,    0.172,  0.266, ..}
list2 = {-5.605, -5.970, -6.505, ..} 

How do I combine the two lists in order to plot points {0.0, -5.605}, {0.172, -5.970}, {0.266, -6.505},....

6
  • 1
    maybe you want to try: Riffle[list1, list2]~Partition~2 Commented May 13, 2013 at 7:21
  • 1
    the user deserves an explanation why this is closed. It is most certainly not "off topic".
    – agentp
    Commented May 13, 2013 at 11:54
  • 1
    To closers: Your opinion about a language (Mathematica (TM))you don't know at all isn't welcomed. You should use your close powers in a wiser way. Commented May 13, 2013 at 14:17
  • possible duplicate of ListPlot With Two Data Sets in Mathematica
    – Mr.Wizard
    Commented May 14, 2013 at 12:07
  • @belisarius I like to think I know a thing or two about the language and I'm still voting to close. ;-p (note the wink)
    – Mr.Wizard
    Commented May 14, 2013 at 12:08

4 Answers 4

20

If you don't like Pinguin Dirk's suggestion try

Transpose[{list1,list2}]
8

yet another..

MapThread[ {#1, #2} & , {list1, list2}]

or if you want to gracefully handle unequal length lists:

MapThread[ {#1, #2} &, Take[#, All, Min @@ Length /@ #] &@{list1, list2} ]
1
  • 4
    You could even simplify this by using MapThread[List , {list1, list2}] Commented Oct 11, 2015 at 8:59
2

Here is another answer that creates a reusable function to pair up two vectors. The function uses a pure function that maps over the length of the shortest vector to create the pairs.

    list1 = {1, 2, 3, 4, 5}; 
    list2 = {13, 18, 20, 18, 13};
    pairUp[xValues_ , yValues_] := ({xValues[[#]], yValues[[#]]}) & /@ 
       Range[Min[Length[xValues], Length[yValues]]];

    pairUp[list1, list2]
    (*
      {{1, 13}, {2, 18}, {3, 20}, {4, 18}, {5, 13}}
    *)

Hope this helps,

Edmund

PS: New to Mathematica and hoping to improve my understanding by trying to answer a few questions on here from time to time.

0

Here you go

Partition[Riffle[x,y],2]
1
  • Please add some explanation to your code such that others can learn from it
    – Nico Haase
    Commented Apr 28, 2020 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.