# Check if number is in list

I have a nested list, but don't know how long the individual lists are. Sometimes they may be 2 numbers long sometimes 1000, so i need to check if theres is a number at position 6 but i doesn't matter what nr it is. i just need to know if there is a nr there or not. So the list might look like this {{1,2,3,4},{1,2}} So there is no number in position 6 or it could look like this {{1,2,3,4,5,6,7},{1,2}} in which case there is a nr and all i need to know is wether there is a nr there. My try doesn't work.

If[list[[10]] > 0]

• Can you provide a small ragged array and mimic what your function is supposed to take as input and return?
– Kuba
Nov 29, 2018 at 15:01
• Optimaly id like a If state where i would say If[list@pos6 == a number, ....,....] Nov 29, 2018 at 15:07
• Could you not simply check the length of the list? If[Length[list]<n,"No number", list[[n]]] ? Nov 29, 2018 at 15:09
• mb its a nested list should have put that in the questio nimmediatly. Can you check the length of sublists in nested lists? Nov 29, 2018 at 15:19

Based on my understanding of your question, the following should work.

Generate 10 lists of random length between 1 and 20. I use integers just because, but you could easily replace it with RandomReal for testing. I assume you have your own list you want to try this on.

list = Table[RandomInteger[{-100, 100}, RandomInteger[{1, 20}]], 10]


Show the length of each list.

Length/@list


Test each list in the list to make sure that it has at least 6 elements AND the sixth element is a number. We have to test for 6 elements first otherwise we will get an error when we ask if the sixth element is a number but there is no sixth element. If the length of the list is less than 6, the first part of the test fails and Mathematica will not evaluate the NumberQ portion. Where I have "Happy days!" is where you can put the code if you successfully find a number in the sixth position, and where I have "No luck :(" is what happens if the test fails.

If[Length[#] >= 6 && NumberQ[#[[6]]], "Happy days!",
"No luck :("] & /@ list

• Is the && NumberQ[#[[6]] condition necessary? From the question I couldn't see why it would be. Nov 30, 2018 at 1:57
• @wilsnunn Yeah, I'm not entirely sure. OP mentions checking if it's a number, and their comment to their own post suggests that might be what they want, but it's not clear to me. If it's known beforehand that the list can only contain numbers and they're really just looking to see if each sublist is long enough, then the second part would not be necessary. Nov 30, 2018 at 2:53

I generate lists of random length.

n = 6; m = 8; lmax = 10;
bill = Table[RandomInteger[{0, m}], n, RandomInteger[lmax]]


I set condition to the value of the number I am looking for. The next line returns the positions in the sublists where it appears. Here it searches for all 4s.

condition = 4
Table[Flatten[Position[bill[[i]], condition]], {i, 1, Length[bill]}]


I am not sure this is what you want, but it fits your current description.

I'm guessing (the question should have a small test case so that we can test our understanding of the problem, now it's not really possible to know if we have the right interpretation or not) you need this:

MatchQ[{Repeated[_, {5}], _Integer, ___}] /@ {
{1, 2, 3},
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 5, 6, 7, 8}
}


{False, True, True}

If all element are always integers, then it's enough to just check that the length is larger or equal to six. It can be done simply enough, e.g. with

lengths = Length /@ {
{1, 2, 3},
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 5, 6, 7, 8}
};
# >= 6 & /@ lengths