# Eliminate imaginary numbers from a list

If I have a list such as:

https://pastebin.com/TGjWnFUH

which has also complex numbers that starts after {95.3282,0.0000150799}. How can I eliminate all data sets that contained those complex numbers (all data after {95.3282,0.0000150799} for this case)?.

Thank you,

• Maybe you want Select[data, Head@#[[2]] == Real &]? – corey979 May 19 at 20:17
• Or Select[data,Im[#[[2]]] < 10^-6 &] to check if the imaginary part is below a threshold – Joshua Schrier May 19 at 20:21
• Another approach is to use pattern matching, e.g., : DeleteCases[data, {_, _Complex}] This looks especially elegant in operator form: DeleteCases[{_, _Complex}]@data – Joshua Schrier May 19 at 20:23
• @Joshua, if you make that an answer, I'll upvote it. ;) – J. M.'s discontentment May 20 at 1:09

You've got two general ways to approach this problem in Mathematica

Select applies a function and preserves entries based on whether that function is true or false. In this approach you could try something like:

Select[data, Im[#[[2]]] < 10^-6 &]

to select cases where the imaginary part (Im) of the second entry in each list item (#[[2]]) is less than some threshold value.

Alternatively, one can use pattern matching. The patten {_, _Complex} matches any item (_) for the first entry in the list and Complex types for the second item. In this approach you could try something like:

DeleteCases[data, {_, _Complex}]

It is potentially more elegant to use the operator form of DeleteCases, as in the following:

DeleteCases[{_, _Complex}]@data

(Thanks to J. M. for the motivation to write this up.)

• DeleteCases[] is clearly the more elegant choice here, but Select[] is more flexible, as you have demonstrated in your first example. – J. M.'s discontentment May 20 at 2:13

Try

v={{95.1222,0.000134014},{95.148,0.000120065},{95.1737,0.000105897},
{95.1994,0.000091497},{95.2252,0.0000768485},{95.2509,0.0000619282},
{95.2767,0.0000467031},{95.3024,0.00003112},{95.3282,0.0000150799},
{95.3797,-0.0000204035+2.54384*10^-6 I},{95.4055,-0.0000364388+4.57472*10^-6 I},
{95.4313,-0.0000527509+6.66189*10^-6 I},{95.4571,-0.0000690327+8.74688*10^-6 I},
{95.4829,-0.000085323+0.0000108347 I},{95.5087,-0.000101651+0.0000129291 I},
{95.5345,-0.000118039+0.0000150328 I},{95.5603,-0.000134501+0.000017148 I},
{95.5862,-0.00015105+0.000019276 I},{95.612,-0.000167694+0.0000214183 I}};
Take[v,Position[v,x_Complex][[1,1]]-1]


which instantly returns the first 9 Real items before the first Complex item.

Remember there are always at least half a dozen different ways of doing anything in Mathematica. Pick the ones that make the most sense to you, try to figure out and understand why they work because that will help you remember them and use them in the future without making too many mistakes.

• Bill. Thank you! This works great ! – John May 19 at 20:19
• This approach depends on the elements with complex values being grouped at the end of the list. A more general approach would be Select[v, FreeQ[#, _Complex] &] – Bob Hanlon May 19 at 22:48
• His subject line was "Eliminate imaginary numbers from a list", but his description was "How can I eliminate all data after {95.3282,0.0000150799}" and he said that the first data item after that contained a complex number. So I used his description and dropped the items starting with the first complex number. Certainly if he changes his data or changes his requirements then the answer will change and if he doesn't change this then this answer does what he described. Moments after I showed this he agreed this method was satisfactory. Maybe he has changed his mind now. – Bill May 20 at 5:27
data // Pick[#,#[[All,2,0]],Real]&


For example:

data={{74.0202,0.000284368},
{74.0404,0.000285019},{94.6603,0.000355035},{94.6859,0.000344053},{94.7115,0.000332932},{94.7371,0.000321672},{94.7628,0.00031027},{94.7884,0.000298723},{94.8141,0.000287028},{94.8397,0.000275184},{94.8654,0.000263187},{94.891,0.000251034},{94.9167,0.000238723},{94.9424,0.000226248},{94.968,0.000213608},{94.9937,0.000200798},{95.0194,0.000187813},{95.0451,0.000174648},{95.0708,0.000161298},{95.0965,0.000147755},{95.1222,0.000134014},{95.148,0.000120065},{95.1737,0.000105897},{95.1994,0.000091497},{95.2252,0.0000768485},{95.2509,0.0000619282},{95.2767,0.0000467031},{95.3024,0.00003112},{95.3282,0.0000150799},{95.3797,-0.0000204035+2.54384*10^-6 I},{95.4055,-0.0000364388+4.57472*10^-6 I},{95.4313,-0.0000527509+6.66189*10^-6 I},{95.4571,-0.0000690327+8.74688*10^-6 I},{95.4829,-0.000085323+0.0000108347 I},{95.5087,-0.000101651+0.0000129291 I},{95.5345,-0.000118039+0.0000150328 I},{95.5603,-0.000134501+0.000017148 I}}


and

data // Pick[#,#[[All,2,0]],Real]&


{{74.0202, 0.000284368}, {74.0404, 0.000285019}, {94.6603, 0.000355035}, {94.6859, 0.000344053}, {94.7115, 0.000332932}, {94.7371, 0.000321672}, {94.7628, 0.00031027}, {94.7884, 0.000298723}, {94.8141, 0.000287028}, {94.8397, 0.000275184}, {94.8654, 0.000263187}, {94.891, 0.000251034}, {94.9167, 0.000238723}, {94.9424, 0.000226248}, {94.968, 0.000213608}, {94.9937, 0.000200798}, {95.0194, 0.000187813}, {95.0451, 0.000174648}, {95.0708, 0.000161298}, {95.0965, 0.000147755}, {95.1222, 0.000134014}, {95.148, 0.000120065}, {95.1737, 0.000105897}, {95.1994, 0.000091497}, {95.2252, 0.0000768485}, {95.2509, 0.0000619282}, {95.2767, 0.0000467031}, {95.3024, 0.00003112}, {95.3282, 0.0000150799}}

Alternatively, as bill has pointed out, the following may be a valid interpretation of the OP requirement:

TakeWhile[data, #[[2,0]]===Real &]