Python set()

Python set() builtin function is used to create a set object in Python using elements from iterable object.

In this tutorial, we will learn about the syntax of Python set() function, and learn how to use this function with the help of examples.

Syntax

The syntax of set() function is

</>
Copy
set([iterable])

where

ParameterRequired/OptionalDescription
iterableOptionalA python iterable object whose elements will be used to create a Set.

Returns

The function returns object of type set.

Examples

1. Create a set from list

In this example, we will create a set from an iterable, say list, using set builtin function.

Python Program

</>
Copy
myList = [2, 4, 6, 8]
mySet = set(myList)
print(mySet)

Output

{8, 2, 4, 6}

2. Create Empty Set

If we do not pass any iterable to set() function, then set() function returns an empty set.

In the following program, we will call set() function without any argument.

Python Program

</>
Copy
mySet = set()
print(mySet)

Output

set()

Conclusion

In this Python Tutorial, we have learnt the syntax of Python set() builtin function, and also learned how to use this function, with the help of Python example programs.