Search code examples
elisp

zip up two list of different lengths in elisp


I have two lists:

(setq x (list "a" "b" "c"))
(setq y (list "1" "2" "3" "4"))

How can I create a list of cons cells (("a" . "1") ("b" . "2") ("c" . "3") ("a" . "4")) with the shorter list recycled?


Solution

  • Here's my take:

    (require 'cl-lib)
    (cl-mapcar #'list (setcdr (last x) x) y)
    

    I'd add a check for which of them is larger, but that would spoil the brevity:).