4

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}}
lio
  • 2,396
  • 13
  • 26

1 Answers1

8

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]]
rhermans
  • 36,518
  • 4
  • 57
  • 149
  • Thank you for the solutions. – lio Jul 05 '18 at 15:58
  • 1
    What is the justification for voting to close this question and then, in effect, posting a duplicate as an answer? – user1066 Jul 05 '18 at 20:12
  • @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. – rhermans Jul 06 '18 at 07:31