I could not find any function to list the keys of an associton list in Emacs documentation. So I wrote a simple one.

(defun my/keys (alist)
    "Return the keys of the given ALIST."
    (let ((value nil)
                (result-list ()))
    (dolist (value alist result-list)
        (setq result-list (cons (car value) result-list)))))

For which the following test evaluates true:

(ert-deftest keys-test ()
    (should (equal (my/keys '((key1 . "foo")
                              ("key2" . "bar")
                              (1 . "bazz")))
                             ;; result:
                             '(1 "key2" key1))))

I do not like reinventing the well are there any Emacs built-in function analogues with mine?


There isn't a built-in for that, hence why libraries like kv.el have been written. Your code can be significantly simplified though:

(defun alist-keys (alist)
  (mapcar 'car alist))
  • 5
    What about map-keys from map.el .
    – politza
    Jun 18 '17 at 14:48
  • Hm, didn't check for that one. Feel free to write another answer!
    – wasamasa
    Jun 18 '17 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.