-2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
syafihakim
  • 81
  • 2
  • 10

2 Answers2

1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Wikipedia has a good page on membership: http://en.wikipedia.org/wiki/Element_%28mathematics%29 – ModulusJoe Nov 04 '13 at 13:03
  • So, membership does not exist in other type of data like list and dict? – syafihakim Nov 04 '13 at 13:04
  • @syafihakim yes it does... to be a member, to be a part of, to be one of.... membership applies to containers... which have "things" in them... Think of it as different named boxes, that you throw things in... The boxes are the containers (list/set/dict/frozenset etc...), and the things you put in the box are the members... – Jon Clements Nov 04 '13 at 13:05
  • @JonClements That's a good anology! Really make me understand it better. – syafihakim Nov 04 '13 at 13:10
0

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?)

Community
  • 1
  • 1
Bolo
  • 11,542
  • 7
  • 41
  • 60