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)













6















Is there a simpler way to remove the last element of a list than this?



(setq list (reverse (cdr (reverse list))))









share|improve this question



















  • 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















6















Is there a simpler way to remove the last element of a list than this?



(setq list (reverse (cdr (reverse list))))









share|improve this question



















  • 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













6












6








6








Is there a simpler way to remove the last element of a list than this?



(setq list (reverse (cdr (reverse list))))









share|improve this question
















Is there a simpler way to remove the last element of a list than this?



(setq list (reverse (cdr (reverse list))))






list deletion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 using nreverse (destructive) rather than reverse (copying), for the sake of efficiency.

    – phils
    May 22 at 4:16












  • 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







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










1 Answer
1






active

oldest

votes


















10














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 nbutlast will 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)





share|improve this answer

























  • 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












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
);



);













draft saved

draft discarded


















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









10














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 nbutlast will 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)





share|improve this answer

























  • 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
















10














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 nbutlast will 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)





share|improve this answer

























  • 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














10












10








10







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 nbutlast will 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)





share|improve this answer















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 nbutlast will 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)






share|improve this answer














share|improve this answer



share|improve this answer








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, 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


















  • 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

















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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

Esgonzo ibérico Índice Descrición Distribución Hábitat Ameazas Notas Véxase tamén "Acerca dos nomes dos anfibios e réptiles galegos""Chalcides bedriagai"Chalcides bedriagai en Carrascal, L. M. Salvador, A. (Eds). Enciclopedia virtual de los vertebrados españoles. Museo Nacional de Ciencias Naturales, Madrid. España.Fotos