Python set()

The set() function creates a set in Python.

Example

list_numbers = [1, 2, 3, 4, 2, 5]

# create set from list numbers_set = set(list_numbers)
print(numbers_set) # Output: {1, 2, 3, 4, 5}

set() Syntax

The syntax of set() is:

set(iterable)

Also Read: Python sets


set() Parameters

set() takes a single optional parameter:


set() Return Value

set() returns:

  • an empty set if no parameters are passed
  • a set constructed from the given iterable parameter

Example 1: Create sets from string, tuple, list, and range