Logo

Programming-Idioms

  • Clojure
  • Js

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.

let new_list = items.filter(function(val,idx,ary) { return idx != i });
items.splice(i,1);

this will modify the original list (kinda)
(defn remove-idx [i items]
    (keep-indexed #(when-not (= i %1) %2) items))

keep in mind this is iterates the whole collection, but it can be used with more complex predicates
with Ada.Containers.Vectors;
use Ada.Containers;
Items.Delete (I);

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