set

Full name
set
Library
Built-in
Syntax

class set([iterable])

Description

The set built-in class returns a set made up of the elements of the iterable included as an argument.

Parameters
  • iterable: Iterable whose elements will be included as elements of the generated set.
Result

The returned result is an object of class set.

Examples

We can obtain a set formed by the elements of a list with the following code:

a = [2, 4, 1, 5]

set(a)

{1, 2, 4, 5}

If we start from two lists, we can obtain the elements common to both by converting them into sets and executing the intersection method of one of them:

a = [1, 2, 3, 4]
b = [2, 4, 5, 6]

set(a).intersection(b)

{2, 4}

Submitted by admin on Mon, 01/18/2021 - 23:10