# If the pattern changes in a nested List

I have a nested list in which part 2 of each sublist repeats itself a number of times before changing, i need an if condition for when part 2 of the next sublist changes from the prevous sublist.

ex. {{2,2},{3,2},{4,2},{2,3},{3,3,}}, part 2 of the 4. sublist changes.

(note that the nested list is much longer and the sublists change a number of times).

I hope my question is understandable.

• No, it is unclear. What do you want to do with this If operator? What is your aim? Nov 23, 2015 at 14:48
• i need a conditon for an if statement dependent on the change of part 2 of a sublist in my nested list. So every time part 2 changes from the previous pattern, add the string "Z0,05" to part 2.. But its only the conditon that i have issues with. Though i think i can use eldos answer. Nov 23, 2015 at 15:11

list = {{2, 2}, {3, 2}, {4, 2}, {2, 3}, {3, 3}};

SplitBy[list, Last]


{{{2, 2}, {3, 2}, {4, 2}}, {{2, 3}, {3, 3}}}

If you want to know the positions where the 2nd element changes:

list = {{2, 2}, {3, 2}, {4, 2}, {2, 3}, {3, 3}, {3, 4}};

Position[Partition[Last /@ list, 2, 1], {a_, b_} /; a != b] + 1


{{4}, {6}}

• thx of the help, i think i can use this. Nov 23, 2015 at 15:07
• How would the code change for a list with 3 element arrays? {{{2, 2, 2}, {3, 2, 3}, {4, 2, 2}}, {{2, 3,2 }, {3, 3, 6}}}
– SPIL
Dec 7, 2016 at 11:13