Emacs Lisp has two families of collections. Sequences are ordered: lists, vectors, strings, bool-vectors and char-tables. Hash tables map keys to values. The list is the language’s oldest structure, and source code is itself written as lists, so this page also explains how the reader sees them. Strings are covered on Strings and characters.
Cons cells and lists
A cons cell has two slots, the car and the cdr. A list is a chain of cells whose last cdr is nil; nil alone is the empty list. A cell whose cdr is not a list is a dotted pair, written (1 . 2). A list whose tail points back into itself is circular; printing it needs print-circle, and the reader writes #1=(a b . #1#) for it. Since Emacs 31, equal compares circular lists without signalling circular-list.
| Area | Functions |
|---|---|
| Predicates | consp, atom, listp, nlistp, null, proper-list-p |
| Access | car, cdr, car-safe, cdr-safe, caar, cadr, cdar, cddr and the longer cXXXr names, nth, nthcdr, last, butlast, nbutlast, safe-length |
| Take and drop | take and ntake (Emacs 29) return the first N elements; drop is an alias for nthcdr (Emacs 30); take-while and drop-while use a predicate (Emacs 31) |
| Construction | cons, list, make-list, append, nconc, number-sequence, ensure-list (Emacs 28) |
| Stack idiom | push and pop on a variable or place; add-to-list and add-to-ordered-list for a variable that holds a set |
| Copy | copy-sequence (shares the elements), copy-tree (copies nested conses, optionally vectors), copy-alist |
| Order | reverse, nreverse (destructive), sort, flatten-tree |
| Removal | remove and remq (copy), delete and delq (may change the list; always use the result), delete-dups; filters by predicate are seq-filter and seq-remove |
| Membership | memq (eq), memql (eql), member (equal), member-ignore-case; each returns the tail that starts at the match |
| Test over all | any and all take a predicate and a list (Emacs 31) |
(let ((stack nil))
(push 'a stack) (push 'b stack) ; stack is (b a)
(list (pop stack) (take 1 stack))) ; => (b (a))
(flatten-tree '(1 (2 (3 nil)) 4)) ; => (1 2 3 4)
(number-sequence 0 10 5) ; => (0 5 10)
sort
(sort SEQ PREDICATE) sorts in place and returns the result, so keep the return value. Emacs 30 adds a second form, (sort SEQ &rest KEYWORD-ARGUMENTS), with :key (a function that gives the sort key), :lessp (default value<), :reverse and :in-place. In this form the default is to sort a copy. Emacs 30 also added the comparison function value<.
(sort '(3 1 2) :reverse t) ; => (3 2 1), argument unchanged
(sort (list "b" "a") :key #'identity) ; => ("a" "b")
Mapping and iteration
| Name | Meaning |
|---|---|
dolist, dotimes | loop over a list or over 0 to N−1; the macros bind a variable and may give a result form |
mapcar | list of the results of a function over a sequence |
mapc | calls for side effects and returns the sequence |
mapcan | like mapcar but joins the (list) results with nconc |
mapconcat | joins results into a string with a separator |
maphash, map-char-table | iterate over a hash table or a char-table |
Association lists and property lists
An alist is a list of (KEY . VALUE) pairs. A plist is a flat list (KEY VALUE KEY VALUE …), written with keywords or symbols.
| Name | Meaning |
|---|---|
assq, assoc | the first pair whose key is eq or equal; assoc takes an optional TESTFN |
rassq, rassoc | search by value |
alist-get | the value for a key, with default and optional test; it is also a place for setf |
assoc-default, assoc-string | look up with a test function, or compare keys as strings |
assq-delete-all, assoc-delete-all, rassq-delete-all | remove pairs; assoc-delete-all takes an optional test (Emacs 27) |
plist-get, plist-put, plist-member | read, write and test a plist; compare with eq unless a predicate argument is given (Emacs 29) |
get, put, symbol-plist | the plist of a symbol |
(let ((al '((a . 1) (b . 2))))
(list (alist-get 'b al) (cdr (assq 'a al)))) ; => (2 1)
(plist-get '("x" 1 "y" 2) "y" #'equal) ; => 2
Sequences and the seq library
The sequence functions work on lists, vectors, strings and bool-vectors: length, elt, copy-sequence, reverse, nreverse, sort, append (to a list), vconcat (to a vector), concat (to a string), sequencep. seq.el adds a generic, extensible interface; its functions put the sequence argument first.
| Name | Meaning |
|---|---|
seq-filter, seq-remove, seq-keep | keep or drop elements by a predicate; seq-keep keeps the non-nil results of a function |
seq-map, seq-map-indexed, seq-mapn, seq-mapcat | return a list of results |
seq-reduce | folds with a function and an initial value |
seq-find, seq-some, seq-every-p, seq-contains-p, seq-position, seq-count | search and test |
seq-uniq, seq-union (Emacs 28), seq-intersection, seq-difference | set operations, with an optional test |
seq-take, seq-drop, seq-subseq, seq-take-while, seq-drop-while, seq-first, seq-rest, seq-elt | slices and element access |
seq-sort, seq-sort-by, seq-reverse, seq-concatenate, seq-into, seq-min, seq-max, seq-partition, seq-group-by, seq-split | ordering, conversion ((seq-into SEQ 'vector)) and grouping |
seq-let, seq-setq, seq-doseq | destructuring binding of a sequence, and a dolist for any sequence |
(seq-let (a b &rest more) '(1 2 3 4)
(list a b more)) ; => (1 2 (3 4))
(seq-reduce #'+ [1 2 3] 0) ; => 6
(seq-into '(?a ?b) 'string) ; => "ab"
map.el gives the same kind of interface for alists, plists, hash tables and arrays: map-elt, map-put!, map-keys, map-values, map-filter, map-apply, map-let.
Arrays, vectors and related types
An array has a fixed length and constant-time access by index. The read syntax of a vector is [a b c]; its elements are not evaluated.
| Type | Syntax and functions |
|---|---|
| vector | [1 2 3]; vector, make-vector, aref, aset, fillarray, vconcat, vectorp |
| bool-vector | #&5"\37" (length, then the bits as a string); make-bool-vector, bool-vector, bool-vector-count-population, bool-vector-union, bool-vector-intersection |
| char-table | no read syntax; make-char-table, char-table-range, set-char-table-range, aref by character code, map-char-table; used for syntax tables, case tables and display tables (Regexps and syntax tables) |
| ring | make-ring, ring-insert, ring-ref, ring-remove, ring-length, ring-elements; a fixed-size history, built on a cons of a vector |
Constant lists, vectors and strings in the source are shared with the code. Changing them with setcar, aset or nreverse is undefined, and the compiler warns about it in Emacs 30. Use copy-sequence or list to build a fresh object first.
Hash tables
A hash table is created with (make-hash-table &rest KEYWORDS).
| Keyword | Meaning |
|---|---|
:test | eq, eql (the default) or equal, or a name defined by define-hash-table-test |
:size | a hint for the initial size |
:weakness | nil, key, value, key-or-value or key-and-value: entries whose key or value is no longer referenced elsewhere can be removed by the garbage collector |
:rehash-size, :rehash-threshold | accepted but without any effect since Emacs 30 |
| Function | Meaning |
|---|---|
gethash, puthash, remhash, clrhash | read (with optional default), store, remove, clear |
maphash | calls a function with each key and value |
hash-table-count, hash-table-p, hash-table-test, hash-table-weakness, copy-hash-table | size and properties |
hash-table-keys, hash-table-values | lists of the keys and the values |
hash-table-contains-p | whether a key is present (Emacs 31) |
define-hash-table-test | defines a test from an equality function and a hash function |
sxhash-eq, sxhash-eql, sxhash-equal, sxhash-equal-including-properties (Emacs 28) | the hash codes used by the built-in tests |
The printed form is #s(hash-table test equal data (k1 v1 k2 v2)), which the reader accepts back. Keys that are strings need :test 'equal, since two equal strings are usually different objects. Since Emacs 27, eql and the hash functions treat NaNs consistently.
(let ((h (make-hash-table :test 'equal)))
(puthash "one" 1 h)
(puthash "two" 2 h)
(list (gethash "one" h) (gethash "three" h 'none) (hash-table-count h)))
;; => (1 none 2)
Not described
Known, not yet covered
- Reference: the full argument lists of the
seq-andmap-functions, the generic functions behind them (cl-defgenericmethods for new sequence types), and thecl-libsequence functions (cl-remove-if,cl-loop). - Reference: the exact printed form of hash tables in each version, including the
size,rehash-sizeandrehash-thresholdentries of older versions. - Reference: the full list of the
cXXXrnames and the additional list utilities ofsubr-xandcl-lib.
See also
- Data types – the types of Lisp objects and their read syntax
- Strings and characters – strings are sequences of characters
- Variables and scope – setf places and
push - Control structures –
dolist,dotimesand loops - Lists – the manual’s chapter on lists
- Sequences, Arrays, and Vectors – the manual’s chapter on sequences
- Hash Tables – the manual’s chapter on hash tables