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$ Commented Apr 11, 2015 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$ Commented Apr 11, 2015 at 20:03
  • 1
    $\begingroup$ I think perhaps Select is what your after, and Count for "length"... $\endgroup$ Commented Apr 11, 2015 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$ Commented Apr 11, 2015 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$ Commented Apr 11, 2015 at 20:31

1 Answer 1

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.