1
$\begingroup$

Suppose I have a list as

list1 = {1,2,3,4,5}

How could I create a table such that the first entry is the lagged entry of this list, i.e:

table1 = {{0,1},{1,2},{2,3},{3,4},{4,5}}

Thank you.

$\endgroup$
1
  • 1
    $\begingroup$ Partition[{0, 1, 2, 3, 4, 5}, 2, 1] $\endgroup$
    – ktm
    Aug 27, 2019 at 21:23

2 Answers 2

4
$\begingroup$

You can use the third and fourth arguments of Partition:

Partition[list1, 2, 1, {-1, -1}, 0]

{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}}

Alternatively,

Transpose[{Most @ #, Rest @ #}] & @ Prepend[list1, 0]

{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}}

$\endgroup$
1
$\begingroup$
a = {1, 2, 3, 4, 5};

Using SequenceCases

SequenceCases[Prepend[0] @ a, {_, _}, Overlaps -> True]

{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}}

$\endgroup$

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.