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

1 Answer 1

3
$\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$

Your Answer

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

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