33 of 151 menu

The union method

The union method returns a set obtained by combining the elements of the sets specified in the parameter.

Syntax

set.union(sets we want to unite)

Example

Let's apply the union method to get the common elements of two sets:

st1 = {'a', 'b', 'c'} st2 = {'1', '2', '3'} res = st1.union(st2) print(res)

Result of code execution:

{'c', '2', 'b', '1', 'a', '3'}

Example

The union method also has a short form:

st1 = {'a', 'b', 'c'} st2 = {'1', '2', '3'} res = st1 | st2 print(res)

Result of code execution:

{'a', '1', 'b', '3', '2', 'c'}

See also

  • method add,
    which adds elements to a set
  • method update,
    which adds elements from other sets
  • method remove,
    which removes elements from a set
  • function len,
    which returns the length of the set
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline