# Finding the index of a specific element in a list

I have searched everywhere to find an answer for this and have come up with nothing!

I have a data file containing two columns, one with wavelengths in nanometer (some with decimal digits, others just with a decimal point) and the other with spectral irradiance values.

I am trying to make a new table that takes as the first column all the wavelengths and in the second column I divide the spectral values by photon energy to get photon flux. However, in this new table I want to stop at a certain index i where the wavelength corresponds to a specific photon energy that I calculated. This is the code I use. It works.

Here is a small portion of the data table:

{280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5, \

SSpecUsed = Drop[Table[{SSpecWavelength[[i]],SSpecIntensity[[i]]/Eph[SSpecWavelength[[i]]]*Subscript[En, G, Si]*q /. numval}, {i, 0, 949, 1}], 1];


My issue is that I manually found the index (949) at which the wavelength is 1107. nm. In a later exercise I need to integrate over this data up to different indices (so for example integrating all the way up to wavelength 2000 and in the next step only up to 1999). So I tried replacing the 949 value by:

Position[SSpecWavelength, Subscript[\[Lambda], G][Eg_]]/.numval


So basically I try using Position to find the index at which the element in the list is equal to the wavelenght that I calculate based on an energy value. When I run it I get empty curly brackets. I even tried this:

Position[SSpecWavelength, 1107.]


and I still get empty curly brackets. I checked and I know for a fact that the element at index 949 is equal to 1107. so why does it not return me the index?

Is there another (better/simpler) way to accomplish what I want?

Thanks in advance, hope the problem is clear!

• Would you please provide a small portion of the data table as an example? That would make live much easier for us. Feb 23, 2019 at 16:35
• You may try something like FirstPosition[SSpecWavelength, _?(# > 1107. &)] to find the first position with a value larger than 1107.. Feb 23, 2019 at 16:37
• In general Position may take floating point numbers more literate than you. The value from the list could be 1107.0000001 or so but only the leading digits are shown. This difference may be small, but Position won't match 1107. with 1107.0000001. Feb 23, 2019 at 16:39
• FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107. Feb 23, 2019 at 16:42
• With FirstPosition[SSpecWavelength, _?(# > 1107. &)][]? Feb 23, 2019 at 17:26

Given

data = {280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5};


you could use

data[[;; Position[data, 282.][[1, 1]]]]


or

data[[;; FirstPosition[data, 282.][]]]


both return

{280., 280.5, 281., 281.5, 282.}

I recommend that you use the 2nd form; i.e., the one using FirstPosition

Now suppose data was

data = {280., 280.5, 281., 281.5, 282.0000001, 282.5, 283., 283.5, 284., 284.5};


Then, although

data[]


prints out as

282.

Position[data, 282.][[1, 1]]


generates the error message To fix this you can write

Position[data, _?(Round[#, .1] == 282. &)]


{{5}}

The same change in its 2nd argument will fix the problem should FirstPostion be used in place of Position.

• Extract[] might be less cumbersome to use than Part[] in this case. Feb 24, 2019 at 2:23
• @J.M.iscomputer-less. Could you expand on that. I can't see any nice way to use Extract because I can't see any equivalent of Span which makes using Part so convenient.. Feb 24, 2019 at 9:15
• Ah, I was thinking of the usage like in the second part of your answer, where you have isolated positions. If you want to use ;; then Part[] is indeed the better choice (or Take[] for that matter, since list[[ ;; n]] and Take[list, n] should both do). Feb 24, 2019 at 14:38