Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python set() Function

Python set() Function

Python built-in function Python built-in function


Description

set() The function creates an unordered set of unique elements, which can be used for relationship testing, removing duplicates, and computing intersections, differences, unions, etc.

Syntax

set syntax:


class set([iterable])

Parameter Description:

Return Value

Returns a new set object.

Example

The following examples demonstrate how to use set:

x

=

set

(

'

runoob

'

)

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 function Python built-in function