{"id":24029,"date":"2021-10-14T09:13:35","date_gmt":"2021-10-14T03:43:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=24029"},"modified":"2021-11-05T17:06:57","modified_gmt":"2021-11-05T11:36:57","slug":"python-program-for-set-union-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-set-union-method\/","title":{"rendered":"Python Program for Set union() Method"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Dictionary popitem() Method<\/a> Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.<\/p>\n The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n set union() Method:<\/strong><\/p>\n The union() method returns a set containing all of the items from the original set as well as all of the items from the specified set (s).<\/p>\n You may enter as many sets as you like, separated by commas.<\/p>\n It does not have to be a set; any iterable object can be used.<\/p>\n If an item appears in more than one set, the result will only contain one instance of that item. (i.e, appears only once)<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n set1:<\/strong> This is required.\u00a0The iterable to combine with. (union)<\/p>\n set2: <\/strong>This is Optional.The other iterable with which to combine. You are free to compare as many iterables as you want. Use a comma to separate each iterable.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Well organized and easy to understand tutorials of us containing Python Set Methods Examples<\/a> will help you work effectively on Python Set Methods.<\/p>\n In the previous article, we have discussed Python Program for Dictionary popitem() Method Python set: Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after …<\/p>\n
\nPython set:<\/strong><\/p>\nGiven First set = {'hello', 'this', 'is', 'btechgeeks'}\r\nGiven second set = {'good', 'morning', 'btechgeeks'}<\/pre>\n
The given first set is :\r\n{'this', 'hello', 'btechgeeks', 'is'}\r\nThe given second set is :\r\n{'btechgeeks', 'morning', 'good'}\r\nThe union set for the given first and second sets is :\r\n{'morning', 'is', 'good', 'this', 'hello', 'btechgeeks'}<\/pre>\n
Here 'hello' is repeated in the both the sets. so, the union() method combines both the sets but prints \r\n'hello' only once(i.e, removes duplicates and writes only once )<\/pre>\n
Given First set = {2, 4, 6, 8, 0, 2, 1}\r\nGiven second set = {3, 1, 4, 4, 0, 2, 5, 1}<\/pre>\n
The given first set is :\r\n{0, 1, 2, 4, 6, 8}\r\nThe given second set is :\r\n{0, 1, 2, 3, 4, 5}\r\nThe union set for the given first and second sets is :\r\n{0, 1, 2, 3, 4, 5, 6, 8}<\/pre>\n
Program for Set union() Method in Python<\/h2>\n
\n
set.union(set1, set2........)<\/pre>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first set as static input and initialize it with some random values.\r\n# Store it in a variable.\r\nfst_sett = {'hello', 'this', 'is', 'btechgeeks'}\r\n# Give the second set as static input and initialize it with some random values.\r\n# Store it in another variable.\r\nscnd_sett = {'good', 'morning', 'btechgeeks'}\r\n# Print the above-given first set.\r\nprint(\"The given first set is :\")\r\nprint(fst_sett)\r\n# Print the above-given second set.\r\nprint(\"The given second set is :\")\r\nprint(scnd_sett)\r\n# Apply union() method to the given first and second set which combines both the\r\n# sets and removes the duplicate items.\r\n# Store it in another variable.\r\nunionn_sett = fst_sett.union(scnd_sett)\r\n# Print the union set for the given first and second sets.\r\nprint(\"The union set for the given first and second sets is :\")\r\nprint(unionn_sett)\r\n<\/pre>\n
The given first set is :\r\n{'this', 'hello', 'btechgeeks', 'is'}\r\nThe given second set is :\r\n{'btechgeeks', 'morning', 'good'}\r\nThe union set for the given first and second sets is :\r\n{'morning', 'is', 'good', 'this', 'hello', 'btechgeeks'}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first set as user input using the set(), map(), input(), and split() functions.\r\n# Store it in a variable.\r\nfst_sett = set(map(int,input(\"Enter some random values separated by spaces = \").split()))\r\n# Give the second set as user input using the set(), map(), input(), and split() functions.\r\n# Store it in another variable.\r\nscnd_sett = set(map(int,input(\"Enter some random values separated by spaces = \").split()))\r\n# Print the above-given first set.\r\nprint(\"The given first set is :\")\r\nprint(fst_sett)\r\n# Print the above-given second set.\r\nprint(\"The given second set is :\")\r\nprint(scnd_sett)\r\n# Apply union() method to the given first and second set which combines both the\r\n# sets and removes the duplicate items.\r\n# Store it in another variable.\r\nunionn_sett = fst_sett.union(scnd_sett)\r\n# Print the union set for the given first and second sets.\r\nprint(\"The union set for the given first and second sets is :\")\r\nprint(unionn_sett)\r\n<\/pre>\n
Enter some random values separated by spaces = 45 55 65 75 85\r\nEnter some random values separated by spaces = 28 45 65 20 10 30\r\nThe given first set is :\r\n{65, 75, 45, 85, 55}\r\nThe given second set is :\r\n{65, 10, 45, 20, 28, 30}\r\nThe union set for the given first and second sets is :\r\n{65, 10, 75, 45, 20, 85, 55, 28, 30}<\/pre>\n
\n