View Problem

Remove an element from a list by index

Given the list [Apple, Banana, Carrot], remove the first element to produce the list [Banana, Carrot]
DiskEdit
ruby
['Apple', 'Banana', 'Carrot'].shift
ExpandDiskEdit
ruby
fruit.delete_at(0)
ExpandDiskEdit
java 1.5 or later
list.remove(0);
DiskEdit
perl
@list = qw(Apple Banana Carrot);
shift @list;
DiskEdit
perl
@list = qw(Apple Banana Carrot);
$offset = 0;
splice(@list, $offset, 1);
ExpandDiskEdit
groovy
// to produce a new list
newlist = list.tail() // for 'Apple' at start
newlist = list - 'Apple' // for 'Apple' anywhere
DiskEdit
groovy
// mutate original list
list.remove(0)
ExpandDiskEdit
scala
val (fl, fr) = fruit.splitAt(0) ; fruit = fl ::: fr.tail
ExpandDiskEdit
scala
fruit = fruit.tail
ExpandDiskEdit
scala
fruit = fruit.drop(1)
ExpandDiskEdit
scala
fruits = fruits.remove(fruits.indexOf(_) == 0)
ExpandDiskEdit
scala
fruits.filterNot(fruits.indexOf(_) == 0)
DiskEdit
python
myList = ['Apple', 'Banana', 'Carrot']
print myList
del myList[0]
# or
myList.pop(0) # returns 'Apple'
print myList
DiskEdit
python 2.7
Easy trick

a=[Apple, Banana, Carrot]
a=a[1:]

remove first n entry:

a=[n:]

remove last n entry=>

a=[0:-n]
ExpandDiskEdit
cpp C++/CLI .NET 2.0
fruit->RemoveAt(0);
ExpandDiskEdit
fsharp
let split_at list n =
let rec split_at' list' n' left right =
match list' with
| [] -> (List.rev left, List.rev right)
| x :: xs -> if n' <= n then split_at' xs (n' + 1) (x :: left) right else split_at' xs (n' + 1) left (x :: right)
split_at' list 0 [] []

// ------

let (_, right) = split_at fruit 0
ExpandDiskEdit
fsharp
let drop list n =
if n <= 0 then
list
else
let (_, right) = split_at list (n - 1)
right

// ------

let result = (drop fruit 1)
ExpandDiskEdit
erlang
Result = tl(List),
ExpandDiskEdit
erlang
[_|Result] = List,
ExpandDiskEdit
erlang
N = 1, {Left, Right} = lists:split(N - 1, List), Result = Left ++ tl(Right),
ExpandDiskEdit
erlang
Result = drop(1, List),
ExpandDiskEdit
ocaml
let delete_at i al =
if i < 0 || i >= List.length al then
invalid_arg "delete_at"
else
let rec del i l =
match l with
| [] -> []
| h::t when i = 0 -> t
| h::t -> h :: del (i-1) t
in
del i al
;;
ExpandDiskEdit
ocaml
let rem_first l =
match l with
| [] -> []
| h::t -> t
;;
DiskEdit
ocaml
List.tl ["Apple"; "Banana"; "Carrot"]
DiskEdit
csharp c# 2.0
class Solution1516
{
static void Main()
{
List<string> fruit = new List<string>() { "Apple", "Banana", "Carrot" };
fruit.RemoveAt(0);
}
}
ExpandDiskEdit
php
$list = array("Apple", "Banana", "Carrot");
unset($list[0]);

// Be aware of that $list[0] isn't set. "Banana" is still $list[1]
ExpandDiskEdit
php
$list = array("Apple", "Banana", "Carrot");
array_shift($list);

// Be aware of that $list[0] is set to "Banana"
DiskEdit
haskell
deleteNth n xs | n > 0 = take (n-1) xs ++ drop n xs

main = print $ deleteNth 1 [1..3]
DiskEdit
haskell
fruit :: [String]
fruit = ["Apple", "Banana", "Carrot"]

main :: IO ()
main = putStrLn $ show $ tail fruit
DiskEdit
clojure
(let [fruit ["Apple" "Banana" "Carrot"]
index 0]
(concat
(take index fruit)
(drop (+ index 1) fruit)))
DiskEdit
clojure
(let [v ["Apple" "Banana" "Carrot"]
index 0]
(into (subvec v 0 n) (subvec v (inc n))))
ExpandDiskEdit
fantom
list := ["Apple", "Banana", "Carrot"]
list.removeAt(0)
ExpandDiskEdit
go
offset := 0
list = append(list[:offset], list[offset+1:]...)

Submit a new solution for ruby, java, perl, groovy ...