34
votes
Accepted
How to concatenate two lists?
You can also just use append.
(append '("a" "b" "c") '("d" "e" "f"))
22
votes
Accepted
Why does a constant in `let` change with repeated calls?
Here is my answer to the identical question, appropriately edited:
The Bad
foo is self-modifying code. This is extremely dangerous. While the variable lst disappears at the end of the let form, its ...
22
votes
Accepted
Is '(a . b) really a list?
It satisfies listp, so in that sense it is a list. listp just tests whether something is a cons or nil (aka ()), on the one hand, or something else, on the other hand.
A proper list or true list (or ...
16
votes
Accepted
Removing the last element of a list
Yes there is:
(setq list (butlast list))
That is a function from subr.el. (Loaded by default. No need to load anything.)
You can also cut a tail with N elements by
(setq list (butlast list N))
A ...
15
votes
Meaning of period in (. 123)
It looks like Emacs simply reads (. 123) as 123, what happened?
That's exactly what happened. To back it up with sources:
if (ch == '.')
{
if (!NILP (tail))
XSETCDR (tail, read0 (...
15
votes
Accepted
add-to-list vs add-hook?
The question you asked
The biggest difference is the last argument:
local for add-hook and
compare-fn for add-to-list.
This means that you have no control over how add-hook decides whether it ...
12
votes
Accepted
Convert 2-item list to a dotted pair?
cons makes a cons cell out of two arguments. apply uses a list as arguments to a function. When their powers combine: (apply #'cons x) converts a two element list into a cons cell (without having to ...
11
votes
Accepted
Melpa - 'Failed to download melpa'
The error seems to be saying that ((\, "http://melpa.org/packages/")) doesn't match the regex "\`https?:"
Let's look at that value, the one being added to package-archives:
'("melpa", "http://melpa....
10
votes
Accepted
What's difference between + and - in the list in org mode
They make no difference, they are just a style of bullet and have no meaning.
There are many different bullet types you can use, for sorted and non-sorted lists.
You can use org-shiftright (s-right) ...
9
votes
Accepted
How to remove / delete nth element of a list
Well, here's a destructive version I'd be happy with:
(defun remove-nth-element (nth list)
(if (zerop nth) (cdr list)
(let ((last (nthcdr (1- nth) list)))
(setcdr last (cddr last))
...
9
votes
Accepted
Are the graphical depictions in docs drawed in manual labour?
Included with emacs is a basic tool called picture-mode:
To edit a picture made out of text characters (for example, a picture of
the division of a register into fields, as a comment in a program)...
8
votes
Accepted
Org Mode - Is there a way to reverse org-list-make-subtree?
The command org-ctrl-c-minus (bound to C-c - by default) turns a region of lines into a list. You need to mark the lines you want to change first, but it works with the headlines in your example as ...
8
votes
Append lists smartly?
append does what the first part of your question asks:
(append &rest SEQUENCES)
Concatenate all the arguments and make the result a list.
The result is a list whose elements are the ...

Dan♦
- 32.6k
8
votes
Accepted
Shortening trees list with (setcdr (nthcdr 2 trees) nil)
Formal answer
In your expanded example you set the variable trees to a new value in:
(setq trees '(pine birch))
That is not what happens in the original example. In the original example really the ...
8
votes
When is the first element in the argument list regarded as a function symbol and when not?
the first element of a list is interpreted as a function symbol
That's the most common case, but it isn't an absolute rule.
First, you need to know that every symbol can have both a function meaning (...
8
votes
Accepted
Filter a list of strings to create a separate list of those that match a given prefix
(seq-filter (apply-partially #'string-prefix-p "a")
'("apple" "pear" "grape" "apricot"))
If you have other similar questions, consult the ...
7
votes
Report duplicates in a list?
Using dash:
(defun find-duplicates (list)
"Return a list that contains each element from LIST that occurs more than once."
(--> list
(-group-by #'identity it)
(-filter (lambda (...
7
votes
Adding many items to a list
There are many different ways. Not sure if there's a particularly "proper" way. There are considerations you should take depending on how you want to add it (destructively, non-destructively) or if ...
7
votes
Accepted
How to subtract 1 from the nth element of a list of numbers?
Using the built-in cl-lib package, you can write the following:
(eval-when-compile
(require 'cl-lib))
(cl-decf (nth 1 x))
Alternatively, in vanilla Elisp:
(setf (nth 1 x) (1- (nth 1 x)))
Be ...
6
votes
How to iterate over JSON list and select one element
You can instruct json-read-from-string to parse JSON arrays as elisp lists by let binding json-array-type to list like so
(let ((json-object-type 'plist)
(json-array-type 'list))
(setq mylist ...
6
votes
How to check if a given string is a substring of an element of a list
The easiest way is to use the Common Lisp compatibility layer:
(require 'cl-seq)
(cl-member "ap" '("foo" "apa" "bar") :test #'string-match)
==> ("apa" "bar")
PS. This is not directly relevant to ...
6
votes
Accepted
Why does the member function return a list's tail rather than t?
This is a general Lisp question - a question about Lisp lists. It is not special to Emacs Lisp.
The answers you are getting are all correct, and they say the same thing, so far. You apparently don't ...
6
votes
add-to-list vs add-hook?
In some cases you can use either function to add an element to a list. That you can do that does not mean that you should, however.
The recommendation by Emacs is to use only add-hook for a hook. ...
6
votes
Accepted
Report duplicates in a list?
I think the easiest way is to use hash tables:
(defun get-duplicates (list &optional test)
(let ((ht (make-hash-table :test (or test #'equal)))
ret)
(dolist (x list)
(incf (...
6
votes
Accepted
How to mapcar uneven lists?
The two built-in "zip-with" functions:
seq-mapn from seq.el (Emacs ≥ 25.1)
cl-mapcar from cl-lib.el (Emacs ≥ 24.3; was mapcar* in cl.el before that)
and the most prominent third-party one:
-...
6
votes
Accepted
How to delete all list elements matching a regexp?
Do you want a new list that has only elements "b1" and "b2"? Or do you want the same list structure, but modified to have removed elements "a1" and "a2"?
@rpluim shows one way to do the former. A ...
6
votes
How can i add same value to every item in list, turning it into alist?
Just syntax error. To create your alist, use to following:
(mapcar (lambda (x) (cons x 'k))
'(a b c d e))
6
votes
Accepted
How to check if two lists share any elements?
You can use seq-intersection, it is documented in its docstring and (elisp) Sequence Functions:
seq-intersection is a compiled Lisp function in `seq.el'.
(seq-intersection SEQUENCE1 SEQUENCE2 &...
6
votes
Accepted
Is there a way to spread a list like in Typescript or Golang?
You can use the backquote mechanism in combination with ,@ to splice the value of a variable into a list; or equivalently, use append:
(setq variable '(1 2 3))
(1 2 3)
(setq another-variable `(-2 -1 ...
6
votes
Accepted
How to map or iterate over a list of files and set the result to `org-agenda-files`?
Without looking up any of the Org variables or functions you refer to, here's a guess at what you're trying to do. They both do the same thing: iterate over a list of file names, expanding them in ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
list × 173org-mode × 17
mapping × 11
string × 10
elisp × 7
quote × 7
functions × 6
alists × 6
sorting × 5
variables × 4
arguments × 4
association-lists × 3
numbers × 3
filtering × 3
read × 3
apply × 3
data-structures × 3
lisp × 3
org-agenda × 2
buffers × 2
debugging × 2
regular-expressions × 2
search × 2
font-lock × 2
flycheck × 2