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.