For a given list, say {94,35,47,29,86,40,46,72,87}, how can I find a list of even and odd numbers.

Here is the output after trying with the suggested solution in the answer.

enter image description here

After putting the number in list as told by @Nasser, I got the intended output.

I am seeking to solve this problem:

Create an Association that lists the Median of the even and odd numbers respectively under the keys True and False. (Hint: Use GroupBy with EvenQ.)

By applying GroupBy, I could now list the even and odd numbers:




True->  {94,86,40,46,72,84,82,100,70,18,94,94,72,4,4,84,60,48,24,10,46,
False->  {35,47,29,87,49,99,7,67,63,13,91,15,39,7,31,67,65,37,93,9,97,

What is needed is the count. True->54, False->49.

With CountsBy as suggested by @sjoerd-smit, I get the count of even and odd numbers in one command.


By Median applied individually, I could get the medians of even and odd numbers in the list.



Output: 49

Similarly, for the list of even numbers.

Might be a way to get the same with one command.

  • 5
    $\begingroup$ Please take a look at GatherBy $\endgroup$
    – Syed
    Sep 3 '21 at 6:36
  • 2
    $\begingroup$ @Syed I recommend always using GroupBy over GatherBy, to be honest. It's just a more powerful function and GatherBy can be unpredictable in the ordering of the sublists. $\endgroup$ Sep 3 '21 at 8:05
  • 4
    $\begingroup$ Dear OP, please start a fresh question for new queries, instead of morphing the question. $\endgroup$
    – Syed
    Sep 3 '21 at 8:40
  • $\begingroup$ @Syed Did not start a new question as from my previous experience, I was told to remove the first question as I formulated the second question based on input from the first one. $\endgroup$ Sep 4 '21 at 5:43
  • 1
    $\begingroup$ The problem I have is that I am no longer sure what you are asking. But maybe you want GroupBy[{94,35,...},EvenQ,Median] ( <|True -> 54, False -> 49|> ) or if you want both Counts and Median, maybe GroupBy[{94,35,...},EvenQ,#]&/@{Length,Median} ( {<|True -> 59, False -> 41|>, <|True -> 54, False -> 49|>} ). But perhaps you require something else? $\endgroup$
    – user1066
    Sep 4 '21 at 8:21

If you only need the counts, then CountsBy is the way to go:

CountsBy[Range[100], EvenQ]

You can also do it with the 3rd argument of GroupBy:

GroupBy[Range[100], EvenQ, Length]

If you want the results in a list in a particular order (say, even then odd), you can use Lookup:

Lookup[CountsBy[Range[100], EvenQ], {True, False}, 0]

I think this was asked before, but I can't find it now searching.

How about

lis = {94, 35, 47, 29, 86, 40, 46, 72, 87}
{Select[lis, EvenQ[#] &], Select[lis, OddQ[#] &]}

Mathematica graphics

I am sure there are many other ways to do this.


A little simpler version as suggested in comment thanks to kglr is

{Select[lis, EvenQ], Select[lis, OddQ]}
  • 3
    $\begingroup$ you can use simpler Select[lis, EvenQ] $\endgroup$
    – kglr
    Sep 3 '21 at 6:54
  • $\begingroup$ Getting the first few elements in black (attached image with the question). Is it due to some mismatch with global variables? $\endgroup$ Sep 3 '21 at 7:21
  • 2
    $\begingroup$ @SplendidDigitalSolutions you need to put the numbers in a list first. a list is { ....} then it will work. You can't just list the number as you did. $\endgroup$
    – Nasser
    Sep 3 '21 at 7:28

Try Cases:

list={94, 35, 47, 29, 86, 40, 46, 72, 87};
twolists=List[Cases[list, Except[_?EvenQ]], Cases[list, Except[_?OddQ]]]
(*{{35, 47, 29, 87}, {94, 86, 40, 46, 72}}*)
Map[Median, twolists]

With order:

List[Sort[Cases[list, Except[_?EvenQ]], Less], Sort[Cases[list, Except[_?OddQ]], Less]]
(*{{29, 35, 47, 87}, {40, 46, 72, 86, 94}}*)
List[Sort[Cases[list, Except[_?EvenQ]], Greater], Sort[Cases[list, Except[_?OddQ]], Greater]]
(*{{87, 47, 35, 29}, {94, 86, 72, 46, 40}}*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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