Emacs Lisp: Remove Elements in List

By Xah Lee. Date: . Last updated: .

Remove Items by Equality Test (For Symbols and Integers)

remq
(remq x list)
  • Remove all x in list.
  • Returns a new list.
  • The original list is unchanged.
  • Comparison is done with eq. [see Emacs Lisp: Test Equality]
(setq xx '(3 4 5))
(remq 4 xx) ; (3 5)
xx ; (3 4 5)
delq
(delq x list)
  • Remove all x in list.
  • The original list is destroyed.
  • Returns a new list.
  • Comparison is done with eq. [see Emacs Lisp: Test Equality]
(setq xx '(3 4 5))

;; always set result to the same var
(setq xx (delq 4 xx)) ; (3 5)

💡 TIP: to remove elements of string elements, use one of the seq-remove, or just mapcar a function yourself. [see Emacs Lisp: Sequence Functions]

Delete Duplicates

delete-dups
(delete-dups list)
  • Remove duplicates, return a new list.
  • Original list is destroyed.
  • Comparison done using equal. [see Emacs Lisp: Test Equality]

💡 TIP: for sequence, use seq-uniq [see Emacs Lisp: Sequence Functions]

(let (xx)
  (setq xx '(3 4 5 3 2))
  (setq xx (delete-dups xx)))
  ;; (3 4 5 2)

Reference

Lisp Data Structure

List

Specialized Lists

Vector

Sequence (List, Vector)

Hash Table