ELisp: 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 ELisp: Equality Test]
(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 ELisp: Equality Test]
(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 ELisp: Sequence Functions]

Delete Duplicates

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

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

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

Reference

Emacs Lisp List

Special Lists

List Structure