3
$\begingroup$

What is the best way to increase the last item of a list by one? I've come up with ++list[[Length[list]]], is there something more efficient?

$\endgroup$
2
  • 6
    $\begingroup$ :) ++list[[-1]] closely related: 42455 $\endgroup$
    – Kuba
    Dec 7 '15 at 19:07
  • $\begingroup$ @Kuba Thanks! Now that I know I also know I should figure out myself :D $\endgroup$ Dec 7 '15 at 20:00
6
$\begingroup$

In my timings with a=Range[100000]

++a[[-1]]

a[[-1]] += 1

a[[-1]] = a[[-1]] + 1

and

++a[[100000]]

a[[100000]] += 1

a[[100000]] = a[[100000]] + 1

all evaluate in the same time.

$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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