ContainsExactly[list1,list2]
yields True if list1 contains exactly the same elements as list2.
ContainsExactly[list2]
is an operator form that yields True when the object to which it is applied contains exactly the same elements as list2.
ContainsExactly
ContainsExactly[list1,list2]
yields True if list1 contains exactly the same elements as list2.
ContainsExactly[list2]
is an operator form that yields True when the object to which it is applied contains exactly the same elements as list2.
Details and Options
- In ContainsExactly[list1,list2], the listi must be lists of expressions, or representations of them, namely objects with heads like SparseArray, QuantityArray, Association or EntityClass.
- ContainsExactly[list1,list2,SameTesttest] applies test to each pair of elements in the listi to determine whether they should be considered the same.
- ContainsExactly[list2][list1] is equivalent to ContainsExactly[list1,list2].
Examples
open all close allBasic Examples (2)
Scope (4)
Check whether certain chemicals contain exactly certain elements:
ContainsExactly[ChemicalData[Entity["Chemical", "Sucrose"], "ElementTypes"], {"C", "H", "O"}]ContainsExactly[ChemicalData[Entity["Chemical", "6Mercaptopurine9BetaDRibofuranoside"], "ElementTypes"], {"C", "H", "O"}]Use ContainsExactly in operator form:
fullweekend = ContainsExactly[{Saturday, Sunday}]fullweekend[{Sunday, Saturday, Friday}]fullweekend[{Sunday, Saturday, Sunday}]Find music works composed in 1982 by Bernie Taupin and Elton John:
EntityList[EntityClass["MusicWork", {"FirstReleaseDate" -> DateObject[{1982}], "Composers" -> ContainsExactly[{Entity["Person", "BernieTaupin::g5988"], Entity["Person", "EltonJohn::8fg2d"]}]}]]ContainsExactly works with associations:
ContainsExactly[<|a -> x, b -> y, c -> x|>, {x, y}]ContainsExactly[<|a -> x, b -> y, c -> x|>, <|1 -> x, 2 -> y|>]Options (2)
SameTest (2)
By default, ContainsExactly considers elements to be the same if they are identical:
list = Subdivide[Quantity[0, "Meters"], Quantity[1, "Meters"], 2]ContainsExactly[list, {Quantity[0, "Meters"], Quantity[1/2, "Meters"], Quantity[1, "Meters"]}]Changes of units are taken into account, as long as they represent the exact same quantity:
ContainsExactly[list, {Quantity[0, "Centimeters"], Quantity[50, "Centimeters"], Quantity[100, "Centimeters"]}]Numerical approximations are not considered as the same object:
ContainsExactly[list, {Quantity[0., "Centimeters"], Quantity[50., "Centimeters"], Quantity[100., "Centimeters"]}]Use Equal as the comparison function to have numerical tolerance:
ContainsExactly[list, {Quantity[0., "Centimeters"], Quantity[50., "Centimeters"], Quantity[100., "Centimeters"]}, SameTest -> Equal]String comparisons are case sensitive by default:
ContainsExactly[{"A", "B", "C"}, {"a", "b", "c"}]Convert to lower case before comparing:
ContainsExactly[{"A", "B", "C"}, {"a", "b", "c"}, SameTest -> (SameQ[ToLowerCase[#1], ToLowerCase[#2]]&)]Applications (1)
Two anagram words or phrases contain the same letters:
letters[string_] := DeleteCases[Characters[string], " "]anagramQ[string1_, string2_] := ContainsExactly[letters[string1], letters[string2]]The word "parsley" is an anagram of the word "replays":
anagramQ["parsley", "replays"]These two phrases contain the same letters:
anagramQ["eleven plus two", "twelve plus one"]Properties & Relations (3)
ContainsExactly[list1,list2] is equivalent to testing for equality as sets:
list1 = {2, 0, 0, 4, 3, 2, 4, 3, 2, 4};list2 = {3, 3, 1, 2, 0, 4, 1, 1, 3, 5};{ContainsExactly[list1, list2], SameQ[Union[list1], Union[list2]]}ContainsExactly is a symmetric function of its arguments. That is, ContainsExactly[list1,list2]==ContainsExactly[list2,list1]:
list1 = {2, 0, 0, 5, 0, 4, 0, 4, 4, 4};list2 = {0, 3, 3, 4, 2, 0, 5, 0, 1, 1};{ContainsExactly[list1, list2], ContainsExactly[list2, list1]}ContainsExactly[list1,list2] is equivalent to And[ContainsAll[list1,list2],ContainsOnly[list1,list2]]:
list1 = {5, 1, 4, 4, 4, 2, 2, 0, 2, 3};list2 = {5, 4, 4, 3, 1, 0, 3, 0, 2, 2};{ContainsExactly[list1, list2], And[ContainsAll[list1, list2], ContainsOnly[list1, list2]]}Related Guides
History
Text
Wolfram Research (2015), ContainsExactly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsExactly.html.
CMS
Wolfram Language. 2015. "ContainsExactly." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContainsExactly.html.
APA
Wolfram Language. (2015). ContainsExactly. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContainsExactly.html
BibTeX
@misc{reference.wolfram_2026_containsexactly, author="Wolfram Research", title="{ContainsExactly}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ContainsExactly.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_containsexactly, organization={Wolfram Research}, title={ContainsExactly}, year={2015}, url={https://reference.wolfram.com/language/ref/ContainsExactly.html}, note=[Accessed: 12-June-2026]}