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
    Commented Dec 7, 2015 at 19:07
  • $\begingroup$ @Kuba Thanks! Now that I know I also know I should figure out myself :D $\endgroup$ Commented Dec 7, 2015 at 20:00

1 Answer 1

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 and acknowledge you have read our privacy policy.

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