# Splitting the List

I want to split a list. There is one set of list with two {x,{a,b}} elements. it's like

list={① {{x1,{a1,b1}},{x2,{a2,b2}}},②{{x1,{a1,b1}},{x2,{a2,b2}}},③{{x1,{a1,b1}},{x2,{a2,b2}}}} (*and so on...*)


What function should I use to split the list when a1 and a2 are the same (a1=a2) and when they are different (a1≠a2)?

I created sample list below.

ListA={{{10, {1, 2}}, {10, {1, 2}}},{{2.5, {1, 2}, {17, {1, 1}}}, {{14, {2, 5}, {10, {1, 1}}},{{12.1, {1, 1},{3,{4,3}}}}


I want to split listA into lista and listb like this.

 lista(*a1=a2*)={{{10, {1, 2}}, {10, {1, 2}}},
{{2.5, {1, 2}}, {17, {1, 1}}}}

listb(*a1≠a2*)={{{14, {2, 5}, {10, {1, 1}}},
{{12.1, {1, 1}},{3,{4,3}}}}

• The definition of ListA has unbalanced opening braces. Please use lower case first letters for user-defined symbols.
– Syed
May 15, 2023 at 11:30
• Thank you for your comment. I edited.
– hare
May 15, 2023 at 15:28
• hare, your edit does not address Syed's comment -- ListA contains unmatched braces.
– kglr
May 15, 2023 at 17:05

ListA =
{{{10, {1, 2}}, {10, {1, 2}}},
{{2.5, {1, 2}}, {17, {1, 1}}},
{{14, {2, 5}}, {10, {1, 1}}},
{{12.1, {1, 1}}, {3, {4, 3}}}};
GatherBy[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]


If you want the data to further show which case is which, maybe you want GroupBy rather than GatherBy:

GroupBy[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]


If you just want lista, then do this:

Select[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]


If you just want listb, then do this:

Select[ListA, Not[Equal @@ Part[#, 1 ;; 2, 2, 1]] &]

• Thank you for your comment. I carried out "GatherBy[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]". Then, {{14, {2, 5}}, {10, {1, 1}}}and{{12.1, {1, 1}}, {3, {4, 3}} were also displayed.This means that (a1=2,a2=1)and(a1=1,a2=4), so it's a1≠a2.
– hare
May 15, 2023 at 16:13
• If possible please tell me what you mean by [#, 1 ;; 2, 2, 1] &]
– hare
May 15, 2023 at 16:14
• I thought you wanted both lista and listb. The result my solution provides is {lista,listb}. May 15, 2023 at 17:01
• In my solutions, I've used GatherBy, GroupBy and Select. Each of these take a function as the second argument that performs the filtering. Equal @@ Part[#, 1 ;; 2, 2, 1] & is a function. I suggest you look at the documentation for Part and Apply and Function. May 15, 2023 at 17:15
• I see. Thank you for your kind explanation! It will be helpful
– hare
May 16, 2023 at 2:57