Table of Contents
In this tutorial, we will learn about the Python set() function with the help of examples.
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}
1. set() Syntax
The syntax of set() is:
set(iterable)
Recommended Reading: Python sets
2. set() Parameters
set() takes a single optional parameter:
- iterable (optional) – a sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be converted into a set.
3. set() Return Value
set() returns:
- an empty set if no parameters are passed
- a set constructed from the given iterable parameter
4. Example 1: Create sets from string, tuple, list, and range
# empty set
print(set())
# from string
print(set('Python'))
# from tuple
print(set(('a', 'e', 'i', 'o', 'u')))
# from list
print(set(['a', 'e', 'i', 'o', 'u']))
# from range
print(set(range(5)))
Output
set()
{'P', 'o', 't', 'n', 'y', 'h'}
{'a', 'o', 'e', 'u', 'i'}
{'a', 'o', 'e', 'u', 'i'}
{0, 1, 2, 3, 4}
Note: We cannot create empty sets using
{ }syntax as it creates an empty dictionary. To create an empty set, we useset().
5. Example 2: Create sets from another set, dictionary and frozen set
# from set
print(set({'a', 'e', 'i', 'o', 'u'}))
# from dictionary
print(set({'a':1, 'e': 2, 'i':3, 'o':4, 'u':5}))
# from frozen set
frozen_set = frozenset(('a', 'e', 'i', 'o', 'u'))
print(set(frozen_set))
Output
{'a', 'o', 'i', 'e', 'u'}
{'a', 'o', 'i', 'e', 'u'}
{'a', 'o', 'e', 'u', 'i'}
6. Example 3: Create set() for a custom iterable object
class PrintNumber:
def __init__(self, max):
self.max = max
def __iter__(self):
self.num = 0
return self
def __next__(self):
if(self.num >= self.max):
raise StopIteration
self.num += 1
return self.num
# print_num is an iterable
print_num = PrintNumber(5)
# creating a set
print(set(print_num))
Output
{1, 2, 3, 4, 5}
Related posts:
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Find the Largest Among Three Numbers
Python Decorators
Python String ljust()
Python String splitlines()
Python List sort()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Dictionary copy()
Python Tuple
Python Set difference()
Python @property decorator
Python callable()
Python float()
Python String rstrip()
Python any()
Python Program to Iterate Over Dictionaries Using for Loop
Python divmod()
Python Program to Check If Two Strings are Anagram
APIs in Node.js vs Python - A Comparison
Python Program to Delete an Element From a Dictionary
Python Program to Check If a String Is a Number (Float)
Python Operators
Python Program to Append to a File
Python Program to Extract Extension From the File Name
Python Dictionary
Python String center()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python for Loop
Python Program to Count the Occurrence of an Item in a List
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Get the Full Path of the Current Working Directory
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili