0

Suppose I have a large alist, where there are multiple items with the same key but same or different values:

'((a . 1)
  (b . 2)
  (a . 3)
  (a . 4)
  (b . 5)
  (b . 6))

Usually when you use alists, you access items with assoc, which returns the first matching item in a list and ignores the rest. So the idiomatic way to treat alists is that every time you want to replace an item in alist with a new value for an old key, you just add a new dotted pair and ignore the old one. So no matter how many duplicates you have, assoc will ignore them. You work via alist API and ignore the implementation details, so to speak. But what if I honestly want to have an alist with all duplicates physically removed? With only the newest dotted pairs remaining in the list, so the above alist would become '((a . 1) (b . 2))?

Any solution for both Common Lisp or Emacs Lisp is acceptable.

Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
  • The question is tagged with both [tag:elisp] and [tag:common-lisp]. The answer provided in the "duplicate" question only covers Emacs Lisp. If you aren't concerned with [tag:common-lisp], it should be removed from the tags. – Joshua Taylor Aug 04 '14 at 17:03
  • For Common Lisp, use `remove-duplicates` or `delete-duplicates` with the keyword arguments `:key #'car :from-end T`, see also: http://clhs.lisp.se/Body/f_rm_dup.htm – marzipankaiser Aug 10 '14 at 10:54

0 Answers0