Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I couldn't find a similar question so I decided to ask this one. I have a nested list with many sublists of equal length. To make things easier let's say it's like that:

list={{2,1,7},{3,9,5},{4,8,6}}

I'd like to get a list of last elements from each sublist, in this case:

{7,5,6}

I'm not sure whether i should think of a pattern to do this or is there a simpler way?

share|improve this question
2  
Here are a few options: 1. Last /@ list 2. list[[All, -1]] 3. list /. {___, i_Integer} :> i –  rm -rf Jan 27 at 16:53
    
list[[;; , -1]] –  RunnyKine Jan 27 at 16:55
1  
Transpose[list][[-1]] –  Pickett Jan 27 at 16:55
    
That was fast - thanks guys! –  Wojciech Jan 27 at 17:01

1 Answer 1

up vote 5 down vote accepted

It is

list = {{2, 1, 7}, {3, 9, 5}, {4, 8, 6}};
list[[All, -1]]
(* {7, 5, 6} *)

or

Last /@ list
(* {7, 5, 6} *)
share|improve this answer
3  
For beginners, the latter command is shorthand for Map[Last[#]&,list] –  Michael Stern Jan 27 at 17:10
1  
Actually Map[Last, list] –  Rojo Jan 27 at 19:39
    
@Rojo Hehe... :-) –  halirutan Jan 27 at 20:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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