w3resource

Python: set() function

set() function

The set() function is used to create a set object.

Read more about set from Python Set and Python Exercises.

Version:

(Python 3.2.5)

Syntax:

set([iterable])

Parameter:

Name Description Required /
Optional
iterable A sequence. Optional

Return value:

An empty set without any parameters.
A set constructed from the given iterable parameter.

Example: Python set() function

# empty set
print(set())

# from string
print(set('Python'))

# from tuple
print(set(('e', 'x', 'e', 'r', 'c', 'i','s','e')))

# from list
print(set(['a', 'e', 'i', 'o', 'u']))

# from range
print(set(range(7)))

Output:

set()
{'h', 'y', 'n', 't', 'o', 'P'}
{'s', 'i', 'r', 'c', 'e', 'x'}
{'i', 'o', 'a', 'e', 'u'}
{0, 1, 2, 3, 4, 5, 6}

Pictorial Presentation:

Python: Built-in-function - set() function

Pictorial Presentation:

Python: Built-in-function - set() function

Python Code Editor:

Previous: round()
Next: setattr()

Test your Python skills with w3resource's quiz



Python: Tips of the Day

How do I check if a list is empty?

For example, if passed the following:

a = []
if not a:
  print("List is empty")

Ref: https://bit.ly/2A4JXx9