Emacs Lisp: Sequence Functions

By Xah Lee. Date: . Last updated: .

Here's a collection of functions that works on Sequence type.

Note, this page does not include specialized functions for Association List nor Property List .

Convert/Join Sequence/List/Vector/String

Sequence Functions

These functions work on any Sequence Type .

Those starting with “seq-” are new in emacs 25.1, released in 2016-09. They are from “seq.el”, and the file is loaded when emacs starts. You may want to explicitly declare (require 'seq).

Some of these provide new functionalities, some gives a unified interface to old functions.

Check Existence

(member x list)
Check if x is in list. If so, return a list starting with the first occurrence of object. Else return nil. Comparison done using equal. [see Emacs Lisp: Test Equality]
(member "4" '("3" "4" "5")) ;; ("4" "5")
(member-ignore-case x list)
same as member, except that x should be a string, and comparison ignores letter-case.
(member-ignore-case "A" '("b" "a")) ; ("a")
(memq x list)
Same as member, but comparison done using eq. Use this if all items are Symbols.
(memql x list)
Same as member, but comparison done using eql.

Delete Elements

(delete x sequence)
  • Remove all x in sequence.
  • The original sequence is destroyed if it's a list.
  • Returns a new sequence.
  • Comparison is done with equal. [see Emacs Lisp: Test Equality]
(setq xx '(3 4 5))

;; always set result to the same var. because original is usually destroyed
(setq xx (delete 4 xx)) ; (3 5)

;; on vector
(setq xx [3 4 5])
(setq xx (delete 4 xx)) ; [3 5]
(delq x sequence)
Same as delete, except the comparison is done with eq. Use this if all items are Symbols.
(setq xx '(3 4 5))

;; always set result to the same var
(setq xx (delq 4 xx)) ; (3 5)
(remove x sequence)
  • Remove all x in sequence.
  • The original sequence is unchanged.
  • Returns a new sequence.
  • Comparison is done with equal. [see Emacs Lisp: Test Equality]
(setq xx '(3 4 5))
(remove 4 xx) ;; (3 5)
xx ; (3 4 5)
(remq x list)
Same as remove, except the comparison is done with eq.
(setq xx '(3 4 5))
(remq 4 xx) ; (3 5)
xx ; (3 4 5)

Delete Duplicates

(delete-dups list)
This function destructively removes all duplicates from list, return a new list. The first one is kept among duplicates. Comparison done using equal. [see Emacs Lisp: Test Equality]
(setq xx '(3 4 5 3 2))
(setq xx (delete-dups xx)) ; (3 4 5 2)

Misc

Reference

Lisp Data Structure


Lisp Basics

Basics

Lisp Data Structure

Function

Lisp Symbol

Lisp Misc

Working with Elisp