4
$\begingroup$

If I have the following list:

{{1, 4}, {1, 3, 8}, {7, 12}, {2, 4, 9, 12}, {5, 7, 18, 19, 22}, {3, 5}}

How can I obtain a list in which each last element of the sublists is removed:

{{1}, {1, 3}, {7}, {2, 4, 9}, {5, 7, 18, 19}, {3}}
$\endgroup$
  • 2
    $\begingroup$ Try Drop[list, 0, -1]. 0 can also be None $\endgroup$ – Coolwater Jul 5 '18 at 16:06
8
$\begingroup$

Most

You are after Most, Mapped over the list. Notice that f/@l is just a short form for Map[f,l].

Most /@ {{1, 4}, {1, 3, 8}, {7, 12}, {2, 4, 9, 12}, {5, 7, 18,
    19, 22}, {3, 5}}
(* {{1}, {1, 3}, {7}, {2, 4, 9}, {5, 7, 18, 19}, {3}} *)

Part

You can use also Part ([[ ]]).

Part[
 {{1, 4}, {1, 3, 8}, {7, 12}, {2, 4, 9, 12}, {5, 7, 18, 19, 22}, {3, 
   5}}
 , All
 , 1 ;; -2
 ]

or

{{1, 4}, {1, 3, 8}, {7, 12}, {2, 4, 9, 12}, {5, 7, 18, 19, 22}, {3, 
   5}}[[All, 1 ;; -2]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for the solutions. $\endgroup$ – lio Jul 5 '18 at 15:58
  • 1
    $\begingroup$ What is the justification for voting to close this question and then, in effect, posting a duplicate as an answer? $\endgroup$ – user1066 Jul 5 '18 at 20:12
  • $\begingroup$ @tomd I didn't vote that it was a duplicate, I voted that it was "easily found in the documentation". But you are correct, I was focused on giving the OP a quick answer, I should have searched for duplicates and I didn't. My wrong. Good thing that Anton Antonov, Bob Hanlon, MarcoB, and Mr.Wizard identified it correctly. $\endgroup$ – rhermans Jul 6 '18 at 7:31

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