Removing the last element of a listHow to iterate over JSON list and select one elementHow to check if a given string is a substring of an element of a listHow to remove / delete nth element of a listReport duplicates in a list?how to specify an infinite list in lisp?How to sort an association list (alist)?How can I convert a list of integers to a string?How to pop an arbitrary element from a list?How do I rotate list elements?Shortening trees list with (setcdr (nthcdr 2 trees) nil)
How to deal with apathetic co-worker?
How to hide an urban landmark?
Passing multiple files through stdin (over ssh)
What is the `some` keyword in SwiftUI?
How can I tell the difference between unmarked sugar and stevia?
How can I get an unreasonable manager to approve time off?
Arriving at the same result with the opposite hypotheses
Universal hash functions with homomorphic XOR property
When conversion from Integer to Single may lose precision
Logarithm of exponential
How to tell your grandparent to not come to fetch you with their car?
Should I give professor gift at the beginning of my PhD?
Is counterpoint still used today?
Payment instructions allegedly from HomeAway look fishy to me
Déjà vu, again?
Impedance ratio vs. SWR
How is water heavier than petrol, even though its molecular weight is less than petrol?
Winning Strategy for the Magician and his Apprentice
How to signal to my players that the following part is supposed to be played on fast forward?
Understanding a current source topology
Share calendar details request from manager's manager
Why did the Tesseract "burn" a hole through Red Skull's plane but not Nick Fury's desk?
Why would future John risk sending back a T-800 to save his younger self?
PhD - Well known professor or well known school?
Removing the last element of a list
How to iterate over JSON list and select one elementHow to check if a given string is a substring of an element of a listHow to remove / delete nth element of a listReport duplicates in a list?how to specify an infinite list in lisp?How to sort an association list (alist)?How can I convert a list of integers to a string?How to pop an arbitrary element from a list?How do I rotate list elements?Shortening trees list with (setcdr (nthcdr 2 trees) nil)
Is there a simpler way to remove the last element of a list than this?
(setq list (reverse (cdr (reverse list))))
list deletion
add a comment |
Is there a simpler way to remove the last element of a list than this?
(setq list (reverse (cdr (reverse list))))
list deletion
1
If doing that, you would likely want to be usingnreverse(destructive) rather thanreverse(copying), for the sake of efficiency.
– phils
May 22 at 4:16
add a comment |
Is there a simpler way to remove the last element of a list than this?
(setq list (reverse (cdr (reverse list))))
list deletion
Is there a simpler way to remove the last element of a list than this?
(setq list (reverse (cdr (reverse list))))
list deletion
list deletion
edited May 21 at 15:12
Drew
49.4k465111
49.4k465111
asked May 21 at 14:14
ToothrotToothrot
1,126414
1,126414
1
If doing that, you would likely want to be usingnreverse(destructive) rather thanreverse(copying), for the sake of efficiency.
– phils
May 22 at 4:16
add a comment |
1
If doing that, you would likely want to be usingnreverse(destructive) rather thanreverse(copying), for the sake of efficiency.
– phils
May 22 at 4:16
1
1
If doing that, you would likely want to be using
nreverse (destructive) rather than reverse (copying), for the sake of efficiency.– phils
May 22 at 4:16
If doing that, you would likely want to be using
nreverse (destructive) rather than reverse (copying), for the sake of efficiency.– phils
May 22 at 4:16
add a comment |
1 Answer
1
active
oldest
votes
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 word about phils' comment:
If it's safe to modify the original list structure, then
nbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
The comment is right and useful but it has to be considered very cautious.
For an exmple the application of nbutlast is safe if the following three conditions are fulfilled:
- if you are in a pure function (you have local non-leaking bindings)
- you generate the list from its elements within the function
- you want to split off the last element at the end of the function (i.e. in the last evaluated form before exiting the function)
Be aware that nbutlast modifies the argument list in the calls of the following functions!
(defun queue (el list)
"Prepend el and remove last element."
(nbutlast (cons el list)))
(defun foo (head list)
"Prepend head and remove last element."
(nbutlast (append head list)))
(setq list (list 2 3 4))
(queue 1 list)
list ;; -> (2 3)
(foo '(1) list)
list ;; -> (2)
If it's safe to modify the original list structure, thennbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
– phils
May 22 at 4:09
Note that behind the scenes, these functions are usingsetcdrto modify thenthcdrwhere N is based on thelengthof the list.
– phils
May 22 at 4:13
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "583"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f50631%2fremoving-the-last-element-of-a-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 word about phils' comment:
If it's safe to modify the original list structure, then
nbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
The comment is right and useful but it has to be considered very cautious.
For an exmple the application of nbutlast is safe if the following three conditions are fulfilled:
- if you are in a pure function (you have local non-leaking bindings)
- you generate the list from its elements within the function
- you want to split off the last element at the end of the function (i.e. in the last evaluated form before exiting the function)
Be aware that nbutlast modifies the argument list in the calls of the following functions!
(defun queue (el list)
"Prepend el and remove last element."
(nbutlast (cons el list)))
(defun foo (head list)
"Prepend head and remove last element."
(nbutlast (append head list)))
(setq list (list 2 3 4))
(queue 1 list)
list ;; -> (2 3)
(foo '(1) list)
list ;; -> (2)
If it's safe to modify the original list structure, thennbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
– phils
May 22 at 4:09
Note that behind the scenes, these functions are usingsetcdrto modify thenthcdrwhere N is based on thelengthof the list.
– phils
May 22 at 4:13
add a comment |
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 word about phils' comment:
If it's safe to modify the original list structure, then
nbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
The comment is right and useful but it has to be considered very cautious.
For an exmple the application of nbutlast is safe if the following three conditions are fulfilled:
- if you are in a pure function (you have local non-leaking bindings)
- you generate the list from its elements within the function
- you want to split off the last element at the end of the function (i.e. in the last evaluated form before exiting the function)
Be aware that nbutlast modifies the argument list in the calls of the following functions!
(defun queue (el list)
"Prepend el and remove last element."
(nbutlast (cons el list)))
(defun foo (head list)
"Prepend head and remove last element."
(nbutlast (append head list)))
(setq list (list 2 3 4))
(queue 1 list)
list ;; -> (2 3)
(foo '(1) list)
list ;; -> (2)
If it's safe to modify the original list structure, thennbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
– phils
May 22 at 4:09
Note that behind the scenes, these functions are usingsetcdrto modify thenthcdrwhere N is based on thelengthof the list.
– phils
May 22 at 4:13
add a comment |
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 word about phils' comment:
If it's safe to modify the original list structure, then
nbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
The comment is right and useful but it has to be considered very cautious.
For an exmple the application of nbutlast is safe if the following three conditions are fulfilled:
- if you are in a pure function (you have local non-leaking bindings)
- you generate the list from its elements within the function
- you want to split off the last element at the end of the function (i.e. in the last evaluated form before exiting the function)
Be aware that nbutlast modifies the argument list in the calls of the following functions!
(defun queue (el list)
"Prepend el and remove last element."
(nbutlast (cons el list)))
(defun foo (head list)
"Prepend head and remove last element."
(nbutlast (append head list)))
(setq list (list 2 3 4))
(queue 1 list)
list ;; -> (2 3)
(foo '(1) list)
list ;; -> (2)
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 word about phils' comment:
If it's safe to modify the original list structure, then
nbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
The comment is right and useful but it has to be considered very cautious.
For an exmple the application of nbutlast is safe if the following three conditions are fulfilled:
- if you are in a pure function (you have local non-leaking bindings)
- you generate the list from its elements within the function
- you want to split off the last element at the end of the function (i.e. in the last evaluated form before exiting the function)
Be aware that nbutlast modifies the argument list in the calls of the following functions!
(defun queue (el list)
"Prepend el and remove last element."
(nbutlast (cons el list)))
(defun foo (head list)
"Prepend head and remove last element."
(nbutlast (append head list)))
(setq list (list 2 3 4))
(queue 1 list)
list ;; -> (2 3)
(foo '(1) list)
list ;; -> (2)
edited May 22 at 12:16
answered May 21 at 14:18
TobiasTobias
16.1k11137
16.1k11137
If it's safe to modify the original list structure, thennbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
– phils
May 22 at 4:09
Note that behind the scenes, these functions are usingsetcdrto modify thenthcdrwhere N is based on thelengthof the list.
– phils
May 22 at 4:13
add a comment |
If it's safe to modify the original list structure, thennbutlastwill be slightly more efficient (n.b. you still need to assign the result back to the variable).
– phils
May 22 at 4:09
Note that behind the scenes, these functions are usingsetcdrto modify thenthcdrwhere N is based on thelengthof the list.
– phils
May 22 at 4:13
If it's safe to modify the original list structure, then
nbutlast will be slightly more efficient (n.b. you still need to assign the result back to the variable).– phils
May 22 at 4:09
If it's safe to modify the original list structure, then
nbutlast will be slightly more efficient (n.b. you still need to assign the result back to the variable).– phils
May 22 at 4:09
Note that behind the scenes, these functions are using
setcdr to modify the nthcdr where N is based on the length of the list.– phils
May 22 at 4:13
Note that behind the scenes, these functions are using
setcdr to modify the nthcdr where N is based on the length of the list.– phils
May 22 at 4:13
add a comment |
Thanks for contributing an answer to Emacs Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f50631%2fremoving-the-last-element-of-a-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If doing that, you would likely want to be using
nreverse(destructive) rather thanreverse(copying), for the sake of efficiency.– phils
May 22 at 4:16