Logo

Programming-Idioms

  • Pascal
  • Rust
  • D

Idiom #29 Remove item from list, by its index

Remove i-th item from list items.
This will alter the original list or return a new list, depending on which is more idiomatic.
Note that in most languages, the smallest valid value for i is 0.

import std.algorithm.mutation;
items = items.remove(i);
list[i] := list[high(list)];
setlength(list,high(list));

2-part Solution:
First the element is overwritten by the last element,
Then the list is reduced by one.
This destroys the original ordering.
items.remove(i)
with Ada.Containers.Vectors;
use Ada.Containers;
Items.Delete (I);

New implementation...
< >
programming-idioms.org