WOLFRAM

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

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

The first list contains only elements in the second list:

Out[1]=1

The first list contains elements not present in the second list:

Out[1]=1

Scope  (4)Survey of the scope of standard use cases

This is one of the longest English words containing only vowels:

Out[1]=1

Use ContainsOnly in operator form:

Out[1]=1
Out[2]=2
Out[3]=3

The number of songs composed either by John Lennon or Paul McCartney, or by both together:

Out[1]=1

ContainsOnly works with associations:

Out[1]=1
Out[2]=2

Options  (2)Common values & functionality for each option

SameTest  (2)

By default, ContainsOnly considers elements to be the same if they are identical:

Out[1]=1
Out[2]=2

Changes of units are taken into account, as long as they represent the exact same quantity:

Out[3]=3

Numerical approximations are not considered as the same object:

Out[4]=4

Use Equal as the comparison function to have numerical tolerance:

Out[5]=5

String comparisons are case sensitive by default:

Out[1]=1

Convert to lower case before comparing:

Out[2]=2

Applications  (1)Sample problems that can be solved with this function

Take the characters of a text, in lower case:

Out[1]=1

They are all letters and basic punctuation symbols:

Out[2]=2

Properties & Relations  (3)Properties of the function, and connections to other functions

Reversing the arguments of ContainsOnly is equivalent to using ContainsAll:

Out[3]=3

ContainsOnly[list1,list2] is equivalent to SubsetQ[list2,list1]:

Out[3]=3

ContainsOnly[{},list] always returns True:

Out[1]=1
Out[2]=2
Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.
Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.

Text

Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.

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.

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

Wolfram Language. (2015). ContainsOnly. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContainsOnly.html

BibTeX

@misc{reference.wolfram_2025_containsonly, author="Wolfram Research", title="{ContainsOnly}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ContainsOnly.html}", note=[Accessed: 20-June-2025 ]}

@misc{reference.wolfram_2025_containsonly, author="Wolfram Research", title="{ContainsOnly}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ContainsOnly.html}", note=[Accessed: 20-June-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_containsonly, organization={Wolfram Research}, title={ContainsOnly}, year={2015}, url={https://reference.wolfram.com/language/ref/ContainsOnly.html}, note=[Accessed: 20-June-2025 ]}

@online{reference.wolfram_2025_containsonly, organization={Wolfram Research}, title={ContainsOnly}, year={2015}, url={https://reference.wolfram.com/language/ref/ContainsOnly.html}, note=[Accessed: 20-June-2025 ]}