Python Set Function


Python set () function is used to create set object from any data type. For example, we use this method to convert tuples or lists to sets. In this Python lesson, we will focus on this function and learn how to convert other data types to python sets. With different examples, you will learn the usage of this python function.


So, to understand better, let’s do some Python set examples that shows how to use this set function of python. Below, we will convert a list to a set with the help of python set function.


You can also check Set Methods of Python.


dwarves2 =['Dwalin', 'Balin', 'Thorin']


The output of this function is a set as expected. In other words, we convert a list to a set with the help of a python function.


{'Balin', 'Thorin', 'Dwalin'}




We can do the similar job for tuples also. We can convert a tupple to a set with the help of this function. Let’s convert the below number tuple to a set with the help of set function.


numbers1 = (3,6,8,12,15)


The output of this set function is like below:


{3, 6, 8, 12, 15}




Let’s give last examples with network devices:


devices = ["router", "switch", "access point", "firewall"]


{'switch', 'firewall', 'router', 'access point'}


Python set function is used to convert any data structure to a set. So, you can use this function in your python codes sometimes to convert other data types to sets. You can do more examples on this lesson to improve your coding skills.


Back to: Python Programming Course > Python Sets

Leave a Reply

Your email address will not be published. Required fields are marked *

IPCisco is the Winner of 2019 “Best Certification Study Journey” Category! We are also Finalist of 2020 & 2021 in Cisco IT Blog Awards!


Python Programming Course

IPCisco on Social Media!