I've got two lists that look like this

list1={{"1/01/2010 6:15", 0.0565625}, {"11/06/2010 0:15",0}, {"11/06/2010 0:30", 0},{"11/06/2010 0:45",0}, {"11/06/2010 1:00", 0}}

list2={{"01/01/2010 06:15", 0.04375}, {"01/01/2010 06:30",0.04375}, {"01/01/2010 06:45", 0.04375}, {"01/01/2010 07:00",0.04375}, {"01/01/2010 07:15", 0.04375}}

What I want to do is create a list that looks like this;

list3={{"1/01/2010 6:15", 0.0565625, 0.04375},{{"01/01/2010 06:30",,0.04375},......}

Either list may have gaps in it. It's not important that all the gaps be filled (if the date is missing from both list1&2 then it can be missing from list3). What is important is that it's fast, the data set is about 100,000 records.

  • $\begingroup$ Will the date strings be listed identically? - i see you have differences: "1/01/2010 6:15" vs "01/01/2010 06:15" $\endgroup$ Commented Nov 29, 2012 at 22:54
  • $\begingroup$ The date strings will be identical. I can convert them to DateLists but I though that would not be necessary because I don't need to fill the gaps. $\endgroup$
    – Cam
    Commented Nov 29, 2012 at 23:04
  • $\begingroup$ Sorry Vitaliy, when I read you question more carefully I understood. You're correct, they are not identical strings (I didn't notice that). Maybe I will need to parse them to a DateList after all. $\endgroup$
    – Cam
    Commented Nov 29, 2012 at 23:19
  • $\begingroup$ Yes I figured that ;) See my answer. $\endgroup$ Commented Nov 29, 2012 at 23:21

2 Answers 2


I confess to being allergic to database operations that require data to be in a particular sort order: it's too easy for huge mistakes to creep in. What is needed here is to turn the source list (say, list1) into a lookup table so it reliably returns its value (second element in the list) when given its key (first element in the list).

To assure reliability of key matches, let's first convert strings into bona fide dates:

l1 = {DateList[First[#]], Last[#]} & /@  list1;
l2 = {DateList[First[#]], Last[#]} & /@  list2;

Now convert the source list into a rule dispatch table:

rules = Dispatch[Flatten[({First[#] -> Sequence @@ #} & /@  l1), 1]]

(This can take some time with 100,000 entries: my system requires almost ten seconds. I consider the wait to be worthwhile. But be careful if the source list is much longer than this!)

These rules can repeatedly be applied to any number of target lists:

l2 /. rules // MatrixForm

$\left( \begin{array}{c} \{\{2010,1,1,6,15,0.\},0.0565625,0.04375\} \\ \{\{2010,1,1,6,30,0.\},0.04375\} \\ \{\{2010,1,1,6,45,0.\},0.04375\} \\ \{\{2010,1,1,7,0,0.\},0.04375\} \\ \{\{2010,1,1,7,15,0.\},0.04375\} \end{array} \right)$

When the target has $100000$ elements and there are $100000$ source elements, this last step still takes only $0.25$ seconds.

  • $\begingroup$ That seems to work. It's the l1 = {DateList[First[#]], Last[#]} & /@ list1; operations that take the time though. I got around 50 seconds on my PC. I had a play around with AbsoluteTime in the hope to speed it up but it didn't make much difference. $\endgroup$
    – Cam
    Commented Nov 30, 2012 at 0:15
  • 1
    $\begingroup$ In some ways it's good that converting strings to dates is the rate-determining step: that means we shouldn't be too concerned about optimizing the join itself, and that gives us the flexibility to craft code that executes correctly in most circumstances. It still looks like a good idea to do the string-to-date conversion, despite the time it takes, because that avoids subtle problems created by potential failures to match two different strings that otherwise represent the same date. Alternatively: do your database operations with a database management system! $\endgroup$
    – whuber
    Commented Nov 30, 2012 at 13:06
  • 1
    $\begingroup$ I up voted this sometime ago, but I have found it so useful for a wide range of problems (beyond date lookups), I wanted to say thanks. $\endgroup$
    – Jagra
    Commented Jun 30, 2013 at 14:59

Your dates are not identical strings. First convert them to numeric dates:

list1d = {DateList[#[[1]]], #[[2]]} & /@ list1;
list2d = {DateList[#[[1]]], #[[2]]} & /@ list2;

Then just

    dat= Reverse[Union[Flatten[#, 1]]]&/@GatherBy[Flatten[{list1d, list2d}, 1], First];

enter image description here

If you need to even out rows - as requested in comments - use PadRight:

PadRight[#, 3] & /@ dat // Grid

enter image description here

  • $\begingroup$ I've parsed the dates using DateList[{#, {"Day", "Month", "Year", "Hour", "Minute"}}] & /@ list1[[All, 1]]. It seems to work but I get manged dates. The output for me looks like this {2010, 15, 6, 1, 0.0565625, 0.04375, 0.}, {2010, 30, 6, 1, 0.04375,0.}, .... $\endgroup$
    – Cam
    Commented Nov 29, 2012 at 23:28
  • $\begingroup$ @Cam I completely updated the answer. $\endgroup$ Commented Nov 29, 2012 at 23:45
  • $\begingroup$ Is there any way to make the length of the rows consistent? Like {{2010,11,6,0,15,0.},0,} or even put a zero in there if it's easier. $\endgroup$
    – Cam
    Commented Nov 30, 2012 at 0:07
  • $\begingroup$ @Cam You need good visual? I am not sure I understand - what do you mean "consistent"? $\endgroup$ Commented Nov 30, 2012 at 0:15
  • $\begingroup$ What I mean is that every row will have 3 elements. It's the difference between {{2010,11,6,0,15,0.},0,} and {{2010,11,6,0,15,0.},0} $\endgroup$
    – Cam
    Commented Nov 30, 2012 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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