ContainsOnly[list1,list2]
yields True if list1 contains only elements that appear in list2.
ContainsOnly[list2]
is an operator form that yields True when the object to which it is applied contains only elements that appear in list2.
ContainsOnly
ContainsOnly[list1,list2]
yields True if list1 contains only elements that appear in list2.
ContainsOnly[list2]
is an operator form that yields True when the object to which it is applied contains only elements that appear in list2.
Details and Options
- In ContainsOnly[list1,list2], the listi must be lists of expressions, or representations of them, namely objects with heads like SparseArray, QuantityArray, Association or EntityClass.
- ContainsOnly[list1,list2,SameTesttest] applies test to each pair of elements in the listi to determine whether they should be considered the same.
- ContainsOnly[list2][list1] is equivalent to ContainsOnly[list1,list2].
Examples
open all close allBasic Examples (2)
Scope (4)
This is one of the longest English words containing only vowels:
ContainsOnly[Characters["euouae"], {"a", "e", "i", "o", "u"}]Use ContainsOnly in operator form:
weekendonly = ContainsOnly[{Saturday, Sunday}]weekendonly[{Saturday, Saturday, Sunday}]weekendonly[{Friday, Saturday, Sunday}]The number of songs composed either by John Lennon or Paul McCartney, or by both together:
EntityValue[EntityClass["MusicWork", {"Composers" -> ContainsOnly[{Entity["Person", "JohnLennon::cpwy3"], Entity["Person", "PaulMcCartney::8wk6t"]}]}], "EntityCount"]ContainsOnly works with associations:
ContainsOnly[<|a -> x, b -> y|>, {x, y, z}]ContainsOnly[<|a -> x, b -> y|>, <|1 -> x, 2 -> y, 3 -> z|>]Options (2)
SameTest (2)
By default, ContainsOnly considers elements to be the same if they are identical:
list = Subdivide[Quantity[0, "Meters"], Quantity[1, "Meters"], 10]ContainsOnly[{Quantity[1/5, "Meters"], Quantity[1/2, "Meters"]}, list]Changes of units are taken into account, as long as they represent the exact same quantity:
ContainsOnly[{Quantity[20, "Centimeters"], Quantity[50, "Centimeters"]}, list]Numerical approximations are not considered as the same object:
ContainsOnly[{Quantity[20., "Centimeters"], Quantity[50., "Centimeters"]}, list]Use Equal as the comparison function to have numerical tolerance:
ContainsOnly[{Quantity[20., "Centimeters"], Quantity[50., "Centimeters"]}, list, SameTest -> Equal]String comparisons are case sensitive by default:
ContainsOnly[{"A", "B", "C"}, {"a", "b", "c"}]Convert to lower case before comparing:
ContainsAny[{"A", "B", "C"}, {"a", "b", "c"}, SameTest -> (SameQ[ToLowerCase[#1], ToLowerCase[#2]]&)]Applications (1)
Take the characters of a text, in lower case:
Length[characters = Characters[ToLowerCase[ExampleData[{"Text", "LoremIpsum"}, "String"]]]]They are all letters and basic punctuation symbols:
ContainsOnly[characters, Join[CharacterRange["a", "z"], {" ", ".", ","}]]Properties & Relations (3)
Reversing the arguments of ContainsOnly is equivalent to using ContainsAll:
list1 = {1, 2, 3, 4, 5};list2 = {4, 0, 4, 4, 1, 1, 3, 1, 5, 5};{ContainsOnly[list1, list2], ContainsAll[list2, list1]}ContainsOnly[list1,list2] is equivalent to SubsetQ[list2,list1]:
list1 = {1, 2, 3, 4, 5};list2 = {1, 1, 1, 5, 5, 3, 4, 1, 0, 4};{ContainsOnly[list1, list2], SubsetQ[list2, list1]}ContainsOnly[{},list] always returns True:
ContainsOnly[{}, {1, 2}]ContainsOnly[{}, {}]Related Guides
History
Text
Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.
CMS
Wolfram Language. 2015. "ContainsOnly." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContainsOnly.html.
APA
Wolfram Language. (2015). ContainsOnly. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContainsOnly.html
BibTeX
@misc{reference.wolfram_2026_containsonly, author="Wolfram Research", title="{ContainsOnly}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ContainsOnly.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_containsonly, organization={Wolfram Research}, title={ContainsOnly}, year={2015}, url={https://reference.wolfram.com/language/ref/ContainsOnly.html}, note=[Accessed: 12-June-2026]}