# Python Language Opérations sur ensembles

## Exemple

avec d'autres ensembles

``````# Intersection
{1, 2, 3, 4, 5}.intersection({3, 4, 5, 6})  # {3, 4, 5}
{1, 2, 3, 4, 5} & {3, 4, 5, 6}              # {3, 4, 5}

# Union
{1, 2, 3, 4, 5}.union({3, 4, 5, 6})  # {1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5} | {3, 4, 5, 6}       # {1, 2, 3, 4, 5, 6}

# Difference
{1, 2, 3, 4}.difference({2, 3, 5})  # {1, 4}
{1, 2, 3, 4} - {2, 3, 5}            # {1, 4}

# Symmetric difference with
{1, 2, 3, 4}.symmetric_difference({2, 3, 5})  # {1, 4, 5}
{1, 2, 3, 4} ^ {2, 3, 5}                      # {1, 4, 5}

# Superset check
{1, 2}.issuperset({1, 2, 3})  # False
{1, 2} >= {1, 2, 3}           # False

# Subset check
{1, 2}.issubset({1, 2, 3})  # True
{1, 2} <= {1, 2, 3}         # True

# Disjoint check
{1, 2}.isdisjoint({3, 4})  # True
{1, 2}.isdisjoint({1, 4})  # False
``````

avec des éléments simples

``````# Existence check
2 in {1,2,3}      # True
4 in {1,2,3}      # False
4 not in {1,2,3}  # True

s = {1,2,3}

s.remove(2)     # s == {1,4}
s.remove(2)     # KeyError!
``````

Les opérations d'ensemble renvoient de nouveaux ensembles, mais ont les versions en place correspondantes:

méthode opération sur place méthode sur place
syndicat s | = t mettre à jour
intersection s & = t intersection_update
différence s - = t difference_update
symétrie_différence s ^ = t symmetric_difference_update

Par exemple:

``````s = {1, 2}
s.update({3, 4})   # s == {1, 2, 3, 4}
``````