Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lisp

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.

(defun remove-item-by-index (index items)
  "Construct a fresh list which is a copy of ITEMS (a list) excluding the item at INDEX."
  (loop for item in items
          as i from 0
          unless (= index i) collect item))

Lisp lists are indexed from 0. Within the LOOP, ITEM is the current item, I is the index of that item, and the two are stepped in parallel.
with Ada.Containers.Vectors;
use Ada.Containers;
Items.Delete (I);

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