Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I have a list of 2D coordinates placed in subarrays of a data structure like the following:

testList = Table[RandomReal[{0, 1000}, {RandomInteger[{0, 32}], 2}], {i, 1, 10}];

And if I sneak in a few entries where 2D coordinates have non-real or non-integer component values (positive or negative Infinity values, etc.) is there a way to quickly prune these coordinates away? In other words, I'd like to go through a data structure like testList and quickly delete any coordinate that has a positive to minus Infinity in it.

Is there a way to make something like the following work?:

Select[testList, {_Real, _Real} &];

What if testList is already flattened by one level? Does the larger context of the data structure matter for Select?

The following works for testList, but is not exactly ideal:

For[i = 1, i <= Length[testList], i++,

  testList[[i]] = 
    Select[testList[[i]], NumberQ[#[[1]]] == True && NumberQ[#[[2]]] == True &];

share|improve this question
up vote 0 down vote accepted

If the list is flattened, you can do:

 Select[testList, #[[1]] \[Element] Reals && #[[2]] \[Element] Reals &]

If it's not flattened, then you can map the select onto the testList:

Select[#, #[[1]] \[Element] Reals && #[[2]] \[Element] Reals &] & /@ testList

For example, if you have terms like {Infinity,Null}, {2,Null}, or {5,-Infinity} in the testList, these terms are rejected.

share|improve this answer

Your Answer


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.