Question

What is membership, searching for this it looks like that term is used not just in Python.

I found it while learning about sets, the documentation says:

Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Was it helpful?

Solution

Membership means that a value is in the set:

member in someset

is True if member is indeed one of the values present in the set. Those values in the set are called 'members'.

The term is a mathematical one and basically is synonimous with 'element', which is what most people use when talking about membership in lists and tuples.

The in operator tests for membership, whatever the container you are testing against:

The operators in and not in test for collection membership

Sets are not the only collections; any container with elements can support membership. In addition to sets, lists and tuples, strings and dictionaries also support membership tests for example.

OTHER TIPS

All containers work with the in operator, which tests membership. That includes built-in containers like tuple, list, set, and dict; and specialized containers from the collections module.

For technical details, see: What exactly are "containers" in python? (And what are all the python container types?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top