{"id":24855,"date":"2021-11-02T09:54:46","date_gmt":"2021-11-02T04:24:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=24855"},"modified":"2021-11-02T09:54:46","modified_gmt":"2021-11-02T04:24:46","slug":"python-set-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-function-with-examples\/","title":{"rendered":"Python set() Function with Examples"},"content":{"rendered":"
set() Function in Python:<\/strong><\/p>\n In Python, the set() function creates a set.<\/p>\n Note:<\/strong> set doesn’t allow duplicate elements and arranges in random order.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set() accepts only one optional parameter:<\/p>\n iterable:<\/strong>\u00a0a sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be converted into a set.<\/p>\n Return Value:<\/strong><\/p>\n The set() function returns:<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Tip:<\/strong> Similarly, you can also try for the set, dictionaries.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n set() Function in Python: In Python, the set() function creates a set. Note: set doesn’t allow duplicate elements and arranges in random order. Syntax: set(iterable) Parameters set() accepts only one optional parameter: iterable:\u00a0a sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be converted into a set. Return Value: The …<\/p>\nset(iterable)<\/pre>\n
\n
Given tuple = (3, 4, 3, 3, 2, 1, 1)\r\nGiven string = \"btechgeeks\"\r\nGiven list = [10, 20, 30, 10, 20, 40]<\/pre>\n
The result after applying set() function on the given tuple = {1, 2, 3, 4}\r\nThe result after applying set() function on the given string = {'b', 'k', 't', 'e', 'g', 'c', 's', 'h'}\r\nThe result after applying set() function on the given list = {40, 10, 20, 30}<\/pre>\n
Given tuple = (25, 35, 45)\r\nGiven string = \"good\"\r\nGiven list = [1, 3, 5, 6, 1]<\/pre>\n
The result after applying set() function on the given tuple = {25, 35, 45}\r\nThe result after applying set() function on the given string = {'o', 'g', 'd'}\r\nThe result after applying set() function on the given list = {1, 3, 5, 6}<\/pre>\n
set() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the tuple as static input and store it in a variable.\r\ngvn_tupl = (3, 4, 3, 3, 2, 1, 1)\r\n# Give the string as static input and store it in another variable.\r\ngvn_str = \"btechgeeks\"\r\n# Give the list as static input and store it in another variable.\r\ngvn_list = [10, 20, 30, 10, 20, 40]\r\n# Pass the given tuple as an argument to the set() function that converts a given\r\n# tuple to a set and returns a set.\r\n# Store it in another variable.\r\nrslt1 = set(gvn_tupl)\r\n# Print the result list after applying set() function on the given tuple.\r\nprint(\"The result after applying set() function on the given tuple = \", rslt1)\r\n# similarly do the same for the given string and list and print it.\r\nrslt2 = set(gvn_str)\r\nprint(\"The result after applying set() function on the given string = \", rslt2)\r\nrslt3 = set(gvn_list)\r\nprint(\"The result after applying set() function on the given list = \", rslt3)\r\n<\/pre>\n
The result after applying set() function on the given tuple = {1, 2, 3, 4}\r\nThe result after applying set() function on the given string = {'b', 'k', 't', 'e', 'g', 'c', 's', 'h'}\r\nThe result after applying set() function on the given list = {40, 10, 20, 30}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the tuple as user input using tuple(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_tupl = tuple(map(int, input(\r\n 'Enter some random Tuple Elements separated by spaces = ').split()))\r\n# Give the string as user input using the input() function and \r\n# store it in another variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given tuple as an argument to the set() function that converts a given\r\n# tuple to a set and returns a set.\r\n# Store it in another variable.\r\nrslt1 = set(gvn_tupl)\r\n# Print the result list after applying set() function on the given tuple.\r\nprint(\"The result after applying set() function on the given tuple = \", rslt1)\r\n# similarly do the same for the given string and list and print it.\r\nrslt2 = set(gvn_str)\r\nprint(\"The result after applying set() function on the given string = \", rslt2)\r\nrslt3 = set(gvn_lst)\r\nprint(\"The result after applying set() function on the given list = \", rslt3)\r\n<\/pre>\n
Enter some random Tuple Elements separated by spaces = 3 4 2\r\nEnter some random string = good\r\nEnter some random List Elements separated by spaces = 6 6 7 8 8\r\nThe result after applying set() function on the given tuple = {2, 3, 4}\r\nThe result after applying set() function on the given string = {'d', 'g', 'o'}\r\nThe result after applying set() function on the given list = {8, 6, 7}<\/pre>\n","protected":false},"excerpt":{"rendered":"