Emacs Lisp: Remove Elements in List

By Xah Lee. Date: . Last updated: .
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)

Delete Duplicates

delete-dups
(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)

Reference

Lisp Data Structure

List

Vector

Sequence (List, Vector)

Hash Table