Logo

Programming-Idioms

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

Idiom #224 Add element to the beginning of the list

Insert the element x at the beginning of the list items.

items.insert(0, x)
items = [x] + items
(def items2 (conj items x))
#include <list>
items.push_front(x);
#include <list>
items.emplace_front(x);
using System.Linq;
items = items.Prepend(x).ToList();
items.Insert(0, x);
items = x ~ items;
items = [x, ...items];
items2 = [x | items]
items = [x, items]
func prepend[S ~[]T, T any](items *S, x ...T) {
	*items = append(x, *items...)
}
items = append(items, x)
copy(items[1:], items)
items[0] = x
items = append([]T{x}, items...)
func prepend[S ~[]T, T any](items *S, x ...T) {
	*items = append(*items, x...)
	copy((*items)[len(x):], *items)
	copy(*items, x)
}
f(x:xs)= x:xs
items2 = x : items
items = [x, ...items];
items.unshift(x);
items.add(0, x);
array_unshift($items, $x);
items.insert(0,x);
unshift @items, $x
items.prepend(x)
items.unshift(x)
use std::collections::VecDeque;
items.push_front(x);
items.Insert(0, x)
Imports System.Linq
items = items.Prepend(x).ToList()

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