I want to iterate over a list with both the index and the value in the loop.

Currently, the code looks like this:

(let ((my-list '(a b c)))
  (cl-loop for index below (length my-list)
           for item = (seq-elt my-list index)
           do (insert (format "index=%s, item=%s\n" index item))))

but calling seq-elt each time is not reasonable.

Another solution looks like this:

(let ((my-list '(a b c)))
  (cl-loop for item being the elements of my-list using (index index)
           do (insert (format "index=%s, item=%s\n" index item))))

But the documentation seems to indicate its obsolete (at least in Common Lisp).

I can't use seq-do-indexed because it was introduced after Emacs 25 with which I would like to keep backward compatibility until 27 is released.

Here is a home-made version that works, but I would prefer a builtin solution if one exists:

(let ((my-list '(a b c))
      (index 0))
  (dolist (item my-list)
    (insert (format "index=%s, item=%s\n" index item))
    (cl-incf index)))

Here is the actual code I'm not satisfied with.

  • 1
    Good question. IMHO, your "homemade version" is clearer and lispier than all of the cl-loop versions. Simple, short, obvious. No need to know another language: "the loop facility". (Just one opinion.)
    – Drew
    Commented Aug 21, 2019 at 15:19

1 Answer 1


This is actually not really different from your "home-made" version, but uses a loop clause to handle the incrementing:

(let ((my-list '(a b c)))
  (cl-loop for index from 0
           for item in my-list
           do (insert (format "index=%s, item=%s\n" index item))))
  • Thank you very much. That was too obvious for me to see it :-). Commented Aug 22, 2019 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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