Easy Tutorial
❮ Python Insertion Sort Python Os Statvfs ❯

Python set() Function

Python Built-in Functions


Description

The set() function creates an unordered collection of unique elements. It can be used for relationship testing, removing duplicate data, and calculating intersections, differences, and unions.

Syntax

set syntax:

class set([iterable])

Parameter description:

Return Value

Returns a new set object.

Example

The following example demonstrates the usage of set:

>>> x = set('tutorialpro')
>>> y = set('google')
>>> x, y
(set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l']))   # Duplicates are removed
>>> x & y         # Intersection
set(['o'])
>>> x | y         # Union
set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])
>>> x - y         # Difference
set(['r', 'b', 'u', 'n'])
>>>

Python Built-in Functions

❮ Python Insertion Sort Python Os Statvfs ❯