(setq whole '(group1 group2(element1 element2(suba subb subc)         
   element3) group3))

I would to print some list of that list, like element2 sublist.

How can i do it?

  • 1
    See C-h f elt to access list elements.
    – JeanPierre
    May 18, 2016 at 9:01
  • Please clarify what you mean by "print." (Also, could you please format your code according to lisp conventions?)
    – Dan
    May 18, 2016 at 9:03
  • something like; (print whole(group2)), displaying the elements of that
    – Steve
    May 18, 2016 at 9:18
  • What i want is to access directly to an element or subelement of a list. So if evaluate (member 'group2 whole) i get all the elements of group2. The question is how to read directly the values (list) of a list of a list. Ie; reading the element2 list from group2 from whole...
    – Steve
    May 18, 2016 at 10:16
  • 2
    How are "the elements of group2" distinguished? As opposed to, say, the elements of group1 or group3. I can't see any clear structure to your form, other than perhaps if item X is followed by item Y, and item Y is also a list, then Y is the "elements of X", otherwise Y is just a sibling of X" -- which you could work with, but isn't very pretty.
    – phils
    May 18, 2016 at 12:27

1 Answer 1


Welcome to the Emacs StackExchange site!

List Structure Clarification

I suspect you made an error in notating your lisp object. It looks like you were maybe trying to add an identifier to the second group? Check out alists and plists for helpful list structures with keys. They may be correspond to the object you had in mind.

Maybe this will help clarify things. Your list object:

'(group1 group2(element1 element2(suba subb subc) element3) group3)

Should, at a minim, have spaces to make it clear that it is indeed lisp code (and not a mutant child of Scheme and JavaScript):

'(group1 group2 (element1 element2 (suba subb subc) element3) group3)

In an expanded form for readability (with newlines and indentation) expresses exactly the same structure which may not be what you intended:

(Preserving the symbol names in the original post; 'group1' 'group2' are misnomers)

   group1 ; first element of the list (accessed by (elt whole 0) since lists are indexed from zero)
   group2 ; second element (another way of accessing is (nth 1 whole) which is functionally the same as (elt whole 1)
   ( ; third element - is the list you were talking about accessing
     element1 ; first element of your nested list
     element2 ; second element of your nested list
     ( ; third element of the third element
       suba ; first element of the third element of the third element
   group3 ; fourth element of the top-level list

Accessing lists

In addition to elt and nth you should also look at using car and cdr. Once you understand these and their many variations you should have a strong grasp of how lists work in lisp.

(car whole) ; evaluates to the 'group1 symbol you have
(cdr whole) ; evaluates to the rest of the list after 'group1
(cadr whole) ; evaluates to the 'group2 symbol you have
(cddr whole) ; evaluates to the 'group2 symbol you have


For printing elements of uncertain type you might want to consider using prin1.

  • Thank you ebpa for your detailed explanation and clarification. Morever, in addition to elt and nth, there's a way to access a nested element by name instead of index number? ie; (traverse 'suba whole) ?
    – Steve
    May 18, 2016 at 16:04
  • @Steve If you are using alists you would use (cdr (assq 'suba whole)) to get the value if you have used the key 'suba to store the value (ex: (setq whole '((suba . "My value")))). Note that lists don't inherently represent an en.wikipedia.org/wiki/Associative_array data structure. alists only have 'keys' or 'names' because they follow a common convention of storing the key as the car of each cons element.
    – ebpa
    May 18, 2016 at 16: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.