Python Set Function

python-set-function-ipcisco-1

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']
print(set(dwarves2))

 

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'}

 

python-set-function-ipcisco-1

 

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)
print(set(numbers1))

 

The output of this set function is like below:

 

{3, 6, 8, 12, 15}

 

python-set-method-ipcisco-2

 

Let’s give last examples with network devices:

 

devices = ["router", "switch", "access point", "firewall"]
print(set(devices))

 

{'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 & Finalist of 2020 in “Best Certification Study Journey ” Category!

Cisco-ITBlogAwards-2019-Winner-IPCisco-k
ITBlogAwards_2020_Badge-Finalist-BestCertStudy-ipcisco

Python Programming Course

Collapse
Expand
IPCisco on Social Media!

Latest Blog Posts

metaverse-jobs-ipcisco.com
Wifi-6-vs-wifi-5-ipcisco.com
16-Reasons-To-Learn-Linux
gns3-vm-ware-installation-guide
reasons-to-have-cisco-ccna-ipcisco-1