{"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>
\nPython set:<\/strong><\/p>\n

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

Given First set = {'hello', 'this', 'is', 'btechgeeks'}\r\nGiven second set = {'good', 'morning', 'btechgeeks'}<\/pre>\n

Output:<\/strong><\/p>\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

Explanation:<\/strong><\/p>\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

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\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

Output:<\/strong><\/p>\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