# Take elements from a list based on two criteria

I am trying to take elements from a list of tuples using 2 criteria. An example of the list is:

list={{-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}


And I need to take the elements that contain one value $$=1$$ and a second value $$\neq1$$. Thus at the end, I will have

    list2={{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}


Ive been trying using select as

list2 = Flatten[Table[Select[list[[i]], # == 1 &, 2]], {i, 1,5}], 1];


but the problem is that the values $$=1$$ do not have a "fixed column". Is there a way to do this avoiding an "if" statement?. Thanks in advance.

Select[list, Count[#, 1] == 1&]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

Also

Pick[list, Lookup[Counts /@ list, 1, 0], 1]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

And alternative ways to use Cases:

Cases[{OrderlessPatternSequence[1, Except[1]]}]@list
Cases[_?(Counts[#][1] == 1&)]@list


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

• Oh, Thanks!. Do you know if the "Select" case can be used as a list?, eg, list[[i]]. This, because I have to track with the case, was not eliminated, For example, at the end, I will have element2-{-1,1}
– mors
May 22, 2019 at 15:08
• @mors, the output from all methods is a list.So, using selected = Select[list, Count[#, 1] == 1&], you can access its parts as, say, selected[[2]] (which is {1, -1}) and selected[[2]] - {-1,1} gives {2, -2}.
– kglr
May 22, 2019 at 15:20
• Thank you, I said it because every element in list is the output of a matrix, so my final goal is to construct llist2with the elements that were not erased from list and the name of the matrix that they correspond. e.g list2={{mat2-{1-1}, mat3-{-1,1}]. So I was wondering if I can use something as list2 = Flatten[ Table[ToString[ tabmatrices[[i]] <> "-" <> ToString[ Select[list[[i]], Count[#, 1] == 1&] ]], {i, 1,5}], 1];
– mors
May 22, 2019 at 16:07

You could use Cases instead:

Cases[list, {1,Except[1]}|{Except[1],1}]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

Paranoid version of kglr's Count solution:

Select[list, Total[Boole[PossibleZeroQ[# - 1]] & /@ #] == 1 &]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

You can also use the following construction:

 lst = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}
Cases[lst, {x_, y_} /; (x == 1 && y != 1) || (y == 1 && x != 1)]


Which may be more helpful in certain situations.

Just a variant:

Reap[Sow[#, Count[#, 1]] & /@ list, 1][[2, 1]]


yields: {{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

l = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}};


Another way using Pick:

Pick[l, Abs@*Subtract @@@ Map[Boole@*(# != 1 &), l, {2}], 1]

(*{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}*)


Or simpler using DeleteCases:

DeleteCases[l, {a_ ..}]

(*{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}*)


Or using SequenceCases:

SequenceCases[l, {s : {a_, b_} /; a != b} :> s]

(*{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}*)

list = {{4, 5}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}};


Pre-define pattern for better comparability

p = {Except[1] ..} | {1, 1};


Using SequenceSplit (new in 11.3)

Join @@ SequenceSplit[list, {p}]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

Using ReplaceAt (new in 13.1)

ReplaceAt[list, p :> Nothing, All]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

Using Delete

Delete[list, Position[list, p, 1]]


{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

Select[list,MatchQ[{OrderlessPatternSequence[1,Except[1]]}]]

(* {{-1,1},{1,-1}, {1, (1 + I)/Sqrt[2]} *)

list = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}

f = Times @@ # == 1 &;

DeleteCases[list, _?f]

Select[list, Not@*f]
`

Result:

{{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}}