Python set() Function

Last Updated : 29 Aug 2026

In python, a set is a built-in class, and this function is a constructor of this class. It is used to create a new set using elements passed during the call. It takes iterable as an argument and returns a new set object. The constructor syntax is given below.

Syntax of set() Function

It has the following syntax:

Parameters

  • iterable: a collection of immutable elements.

Return Values

It returns a new set.

Examples of Python set() Function

Let's see some examples of set() function to understand it's functionality.

Example 1: Creating Sets from Strings using set()Function

This example demonstrates how the set() function is used to create sets and extract unique elements from strings.

Python

Execute Now

Output:

set()
{'1', '2'}
{'a', 'n', 'v', 't', 'j', 'p', 'i', 'o'}

Example 2: Using set() Function with List, Tuple, and Dictionary

This example demonstrates how the set() function can be used with different data types like lists, tuples, and dictionaries.

Python

Execute Now

Output:

{'15', '13', '12'}
{'n', 'v', 'a', 'j', 'p', 't', 'o', 'i'}
{1, 2, 3}

Example 3: Using set() and filter() to Store Unique Even Values

This example demonstrates how the set() function can be combined with filter() to store only even numbers as unique elements.

Python

Execute Now

Output:

{8, 2, 4, 6}