Programming-Idioms

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

Idiom #224 Add element to the beginning of the list

Insert element x at the beginning of list items.

items.prepend(x)
items.unshift(x)
#include <list>
items.emplace_front(x);
#include <list>
items.push_front(x);
items = [x, items]
items = append([]T{x}, items...)
items = append(items, x)
copy(items[1:], items)
items[0] = x
items2 = x : items
items.unshift(x);
items = [x, ...items];
items.insert(0,x);
unshift @items, $x
items = [x] + items
use std::collections::VecDeque;
items.push_front(x);

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms