Python Set Function

python-set-function-ipcisco-1

Python set () fucntion is used to create set object form any data type. With this method, we use this method to convert tuples or lists to sets for example.

 

Let’s show some examples that shows how to use set methos of python.

 

Below, we will convert a list to a set with the help of python set function.

 

dwarves2 =['Dwalin', 'Balin', 'Thorin']
print(set(dwarves2))

 

The output of this function is a set as expected.

 

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

 

python-set-function-ipcisco-1

 

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

 

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 tuple. So, you can use this fucntion in your python codes sometimes.

 

Back to: Python Programming Course > Python Sets

Leave a Reply

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

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k