ContainsAny[list1,list2]
yields True if list1 contains any of the elements of list2.
ContainsAny[list2]
is an operator form that yields True when the object to which it is applied contains any of the elements in list2.
ContainsAny
ContainsAny[list1,list2]
yields True if list1 contains any of the elements of list2.
ContainsAny[list2]
is an operator form that yields True when the object to which it is applied contains any of the elements in list2.
Details and Options
- In ContainsAny[list1,list2], the listi must be lists of expressions, or representations of them, namely objects with heads like SparseArray, QuantityArray, Association or EntityClass.
- ContainsAny[list1,list2,SameTesttest] applies test to each pair of elements in the listi to determine whether they should be considered the same.
- ContainsAny[list2][list1] is equivalent to ContainsAny[list1,list2].
Examples
open all close allBasic Examples (2)
Scope (5)
Take a random integer between 0 and 1000:
RandomInteger[1000]Check whether it contains any even digit:
ContainsAny[IntegerDigits[%], {0, 2, 4, 6, 8}]Use ContainsAny in operator form:
weekend = ContainsAny[{Saturday, Sunday}]weekend[{Monday, Thursday, Tuesday}]weekend[{Monday, Saturday, Tuesday}]Find tunnels in several countries:
EntityList[EntityClass["Tunnel", {"Countries" -> ContainsAny[{Entity["Country", "HongKong"], Entity["Country", "China"]}]}]]Find the movies directed by Steven Spielberg whose cast includes any of the actors in the given list:
EntityList[EntityClass["Movie", {"Director" -> Entity["Person", "StevenSpielberg::76z25"], "Cast" -> ContainsAny[{Entity["Person", "HarrisonFord::y8237"], Entity["Person", "TomHanks::57d8x"]}]}]]ContainsAny works with associations:
ContainsAny[<|a -> x, b -> y, c -> z|>, {y, Y}]ContainsAny[<|a -> x, b -> y, c -> z|>, <|1 -> y, 2 -> Y|>]Options (2)
SameTest (2)
By default, ContainsAny considers elements to be the same if they are identical:
list = Subdivide[Quantity[0, "Meters"], Quantity[6, "Meters"], 3]ContainsAny[list, {Quantity[1, "Meters"], Quantity[2, "Meters"]}]Changes of units are taken into account, as long as they represent the exact same quantity:
ContainsAny[list, {Quantity[100, "Centimeters"], Quantity[200, "Centimeters"]}]Numerical approximations are not considered as the same object:
ContainsAny[list, {Quantity[100., "Centimeters"], Quantity[200., "Centimeters"]}]Use Equal as the comparison function to have numerical tolerance:
ContainsAny[list, {Quantity[100., "Centimeters"], Quantity[200., "Centimeters"]}, SameTest -> Equal]String comparisons are case sensitive by default:
ContainsAny[{"A", "B", "C"}, {"a", "b"}]Convert to lower case before comparing:
ContainsAny[{"A", "B", "C"}, {"a", "b"}, SameTest -> (SameQ[ToLowerCase[#1], ToLowerCase[#2]]&)]Applications (1)
Take the list of words in the English translation of the first volume of Don Quixote:
Length[words = ExampleData[{"Text", "DonQuixoteIEnglish"}, "Words"]]It contains the names of some of the modern European countries:
ContainsAny[words, CanonicalName[CountryData["Europe"]]]But it cannot contain countries in South America, which did not exist then:
ContainsAny[words, CanonicalName[CountryData["SouthAmerica"]]]Properties & Relations (6)
ContainsAny[list1,list2] is equivalent to testing for nonempty intersection of the lists:
list1 = {5, 8, 8, 3};list2 = {9, 10, 7, 2};{ContainsAny[list1, list2], Intersection[list2, list1] =!= {}}ContainsAny[list1,list2] is equivalent to IntersectingQ[list1,list2]:
list1 = {7, 1, 10, 5};list2 = {5, 5, 9, 10};{ContainsAny[list1, list2], IntersectingQ[list2, list1]}ContainsAny is a symmetric function of its arguments. That is, ContainsAny[list1,list2]==ContainsAny[list2,list1]:
list1 = {3, 3, 10, 9};list2 = {4, 3, 0, 5};{ContainsAny[list1, list2], ContainsAny[list2, list1]}Negation of ContainsAny[list1,list2] is equivalent to ContainsNone[list1,list2]:
list1 = {4, 3, 5, 7}list2 = {8, 10, 9, 5}{Not[ContainsAny[list1, list2]], ContainsNone[list2, list1]}ContainsAny[list,{}] always returns False:
ContainsAny[{1, 2}, {}]ContainsAny[{}, {}]When working with lists of strings, there is case sensitivity:
ContainsAny[{"A", "B"}, {"a", "b", "c"}]ContainsAny[{"A", "B"}, {"A", "b", "c"}]Related Guides
History
Text
Wolfram Research (2015), ContainsAny, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsAny.html.
CMS
Wolfram Language. 2015. "ContainsAny." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContainsAny.html.
APA
Wolfram Language. (2015). ContainsAny. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContainsAny.html
BibTeX
@misc{reference.wolfram_2026_containsany, author="Wolfram Research", title="{ContainsAny}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ContainsAny.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_containsany, organization={Wolfram Research}, title={ContainsAny}, year={2015}, url={https://reference.wolfram.com/language/ref/ContainsAny.html}, note=[Accessed: 15-June-2026]}