{"id":3393,"date":"2026-11-14T06:26:21","date_gmt":"2026-11-14T06:26:21","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3393"},"modified":"2026-11-14T06:26:36","modified_gmt":"2026-11-14T06:26:36","slug":"python-set-union","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-union\/","title":{"rendered":"Python Set union() Method with Examples"},"content":{"rendered":"<p>Hello friends! Suppose you have two lists of names and you want one combined list with no name repeated. In Python, sets make this very easy. The <code>union()<\/code> method combines two or more sets and returns a <strong>new set<\/strong> containing every item that appears in any of them. Since sets never hold duplicates, an item that appears in more than one set is kept only once. The original sets are not changed. Let&#8217;s see how it works.<\/p>\n<pre class=\"wp-block-code\"><code>set1.union(set2, set3, ...)\nset1 | set2 | set3        # operator form<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>a = {1, 2, 3}\nb = {3, 4, 5}\nprint(a.union(b))\nprint(a | b)\nprint(a)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 2, 3, 4, 5}\n{1, 2, 3, 4, 5}\n{1, 2, 3}<\/code><\/pre>\n<p>The common item <code>3<\/code> appears only once in the result, and <code>a<\/code> is unchanged.<\/p>\n<p>And you are not limited to just two sets.<\/p>\n<h2>Union of More Than Two Sets<\/h2>\n<pre class=\"wp-block-code\"><code>c = {5, 6}\nprint(a.union(b, c))\nprint(a | b | c)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}<\/code><\/pre>\n<h2>union() vs the | Operator<\/h2>\n<p>You may have seen the <code>|<\/code> symbol used for the same job. They give the same result, with one difference: <code>union()<\/code> accepts any iterable (list, tuple, string, range), while <code>|<\/code> needs sets on both sides.<\/p>\n<pre class=\"wp-block-code\"><code>print(a.union([3, 7, 7], (8,)))\n\ntry:\n    print(a | [3, 7])\nexcept TypeError as e:\n    print(\"TypeError:\", e)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 2, 3, 7, 8}\nTypeError: unsupported operand type(s) for |: 'set' and 'list'<\/code><\/pre>\n<h2>Changing a Set in Place: update() and |=<\/h2>\n<p>So far, <code>union()<\/code> has always given us a new set. But what if you want to add the items to an existing set instead of creating a new one, use <code>update()<\/code> or <code>|=<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>s = {1, 2}\ns.update([2, 3], {4})\nprint(s)\ns |= {5}\nprint(s)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 2, 3, 4}\n{1, 2, 3, 4, 5}<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-update\/\">Python set update()<\/a><\/p>\n<p>Now let&#8217;s use all this in a small real-life example.<\/p>\n<h2>Practical Example: All Students in Two Batches<\/h2>\n<pre class=\"wp-block-code\"><code>morning = {\"Riya\", \"Aman\", \"Kabir\"}\nevening = {\"Aman\", \"Neha\", \"Riya\", \"Zoya\"}\nall_students = morning | evening\nprint(len(all_students), sorted(all_students))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>5 ['Aman', 'Kabir', 'Neha', 'Riya', 'Zoya']<\/code><\/pre>\n<p>Students who attend both batches are counted once. Note that a set has no fixed order, so <code>sorted()<\/code> is used to print the names in a predictable order.<\/p>\n<h2>Key Points<\/h2>\n<p>Let&#8217;s quickly revise what we learned:<\/p>\n<ul>\n<li><code>union()<\/code> always returns a new set, even when called with no arguments (it then returns a copy).<\/li>\n<li>Duplicates are removed automatically.<\/li>\n<li>The order of items in a set is not guaranteed, so never rely on it.<\/li>\n<li>Use <code>update()<\/code> or <code>|=<\/code> to change the original set instead of creating a new one.<\/li>\n<\/ul>\n<h2>Practice Questions<\/h2>\n<p>Try these yourself before you look at the answers:<\/p>\n<ol>\n<li><code>print({1, 2} | {2, 3} | {3, 4})<\/code><\/li>\n<li><code>print(len({\"a\", \"b\"} | set(\"abc\")))<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>{1, 2, 3, 4}<\/code>. 2) <code>3<\/code>, because <code>set(\"abc\")<\/code> is <code>{'a', 'b', 'c'}<\/code> and the union has three distinct letters.<\/p>\n<p>So friends, remember the main points: <code>union()<\/code> does not change the original set, it returns a new one, and to modify the original you use <code>update()<\/code> or <code>|=<\/code>. It also accepts any iterable, such as a list, while the <code>|<\/code> operator needs sets on both sides. And while union gives you items present in <em>any<\/em> of the sets, <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/\">intersection<\/a> gives only the items present in <em>all<\/em> of them. Try the examples in your Python editor and you will get comfortable with sets in no time.<\/p>\n<p>More set methods: <a href=\"https:\/\/gyanipandit.com\/programming\/set-methods-in-python\/\">Set methods in Python<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/\">intersection()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-difference\/\">difference()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-add\/\">add()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Suppose you have two lists of names and you want one combined list with no name repeated. In Python, sets make this very easy. The union() method combines two or more sets and returns a new set containing every item that appears in any of them. Since sets never hold duplicates, an item &#8230; <a title=\"Python Set union() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-set-union\/\" aria-label=\"Read more about Python Set union() Method with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3393","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3393"}],"version-history":[{"count":4,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3393\/revisions"}],"predecessor-version":[{"id":6136,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3393\/revisions\/6136"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}