0
$\begingroup$
TakeWhile[{1, 2, 3, 4, 5}, # > 3 &]

gives me {}.

Similarly LengthWhile[{1, 2, 3, 4, 5}, # > 3 &] gives me 0.

I don't know what is going wrong here. Why is Greater not working with TakeWhile and LengthWhile?

I am using Mathematica 10.0.2.

$\endgroup$
5
  • 3
    $\begingroup$ Umm, since the first value is not greater than 3, there are no values to take. No values = 0 values = empty list. What's the question? $\endgroup$
    – ciao
    Apr 11 '15 at 19:57
  • $\begingroup$ sorry my bad, i was thinking it should run over the entire list and give me a subset of matched elements. $\endgroup$
    – user13892
    Apr 11 '15 at 20:03
  • 1
    $\begingroup$ I think perhaps Select is what your after, and Count for "length"... $\endgroup$
    – ciao
    Apr 11 '15 at 20:04
  • $\begingroup$ You can format inline code and code blocks by selecting it and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. $\endgroup$
    – Michael E2
    Apr 11 '15 at 20:31
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Apr 11 '15 at 20:31
3
$\begingroup$

I trust, you have figured out the response to your "why" question by reading the comments. For your convenience here some very common ways for selecting elements from a list.

Cases[{1, 2, 3, 4, 5}, x_?(# > 3 &)]
Cases[{1, 2, 3, 4, 5}, x_ /; x > 3]
Select[{1, 2, 3, 4, 5}, # > 3 &]

(*{4,5}*)

Welcome to Mathematica !!!

$\endgroup$

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