Logo

Programming-Idioms

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

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.

local removedItem = table.remove(items,i)
with Ada.Containers.Vectors;
use Ada.Containers;
Items.Delete (I);
(defn remove-idx [i items]
    (keep-indexed #(when-not (= i %1) %2) items))
items.erase (items.begin () + i);
using System.Collections.Generic;
items.RemoveAt(i);
import std.algorithm.mutation;
items = items.remove(i);
items.removeAt(i);
List.delete_at(items, i)
{Left, [_|Right]} = lists:split(I-1, Items),
Left ++ Right.
integer, allocatable, dimension(:) :: items
items = [items(:i-1), items(i+1:)]
import "slices"
items = slices.Delete(items, i, i+1)
copy(items[i:], items[i+1:])
items[len(items)-1] = nil
items = items[:len(items)-1]
items = append(items[:i], items[i+1:]...)
take i items ++ drop (1 + i) items
items.splice(i,1);
let new_list = items.filter(function(val,idx,ary) { return idx != i });
items.remove(i);
import java.util.List;
items.remove(i);
items.removeAt(i)
@import Foundation;
[items removeObjectAtIndex:i];
unset($items[$i]);
list[i] := list[high(list)];
setlength(list,high(list));
$removed_element = splice @items, $i, 1;
items.pop(i)
del items[i]
items.delete_at(i) 
items.remove(i)
val without = {
  val (left, right) = items.splitAt(i)
  left ++ right.drop(1)
}
(define (removeElementByIndex L i)
  (if (null? L)
      null
      (if (= i 0)
          (cdr L)
          (cons (car L) (removeElementByIndex (cdr L) (- i 1)))
       )
    )
)
items removeAt: i.

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