cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

How to check if a list contains any values from another list?

miguello
Level VI

Is there an elegant one-liner for that rather than iterate through the second list using Contains() function?

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User


Re: How to check if a list contains any values from another list?

You can loop through one of the lists and use Contains() to see if the specific list item is found anywhere in the other list.

Or, you can convert both of the lists into separate Associative Arrays and then check for the Intersect between the two arrays.

 

Names Default To Here( 1 );

listA = {"a", "b", "c"};

listB = {"d", "c", "f"};

inBothList = {};

For Each( {member}, listA,
	If( Contains( listB, member ),
		Insert Into( inBothList, member )
	)
);

Show( inBothList );

arrayA = Associative Array( listA );
arrayB = Associative Array( listB );

arrayA << intersect( ArrayB);

show( arrayA );
Jim

View solution in original post

3 REPLIES 3
miguello
Level VI


Re: How to check if a list contains any values from another list?

Not one-liner, but more elegant than a loop:

A = {1, 2, 3, 4, 5};
B = {3, 6, 7};
check = Associative Array(A);
check << Intersect(Associative Array(B));
N Items(check);
txnelson
Super User


Re: How to check if a list contains any values from another list?

You can loop through one of the lists and use Contains() to see if the specific list item is found anywhere in the other list.

Or, you can convert both of the lists into separate Associative Arrays and then check for the Intersect between the two arrays.

 

Names Default To Here( 1 );

listA = {"a", "b", "c"};

listB = {"d", "c", "f"};

inBothList = {};

For Each( {member}, listA,
	If( Contains( listB, member ),
		Insert Into( inBothList, member )
	)
);

Show( inBothList );

arrayA = Associative Array( listA );
arrayB = Associative Array( listB );

arrayA << intersect( ArrayB);

show( arrayA );
Jim
jthi
Super User


Re: How to check if a list contains any values from another list?

I usually use either For Each + Break or Associative Arrays but there are many different methods where you can use functions a bit "incorrectly" such as Transform Each ("one-liner" such as this are usually horrible ideas (in my opinion) as they tend to be difficult to read)

 

Names Default To Here( 1 );

A = {1, 2, 3, 4, 5};
B = {3, 6, 7};

r = Eval(Transform Each({item}, A, Output("Expression", "Any"), Contains(B, item)));

It also depends on what you are comparing: strings, integers, floats, lists, ... as JMP lists can basically contain anything.

 

-Jarmo