YouTip LogoYouTip

Ref Set Union

# Python3.x Python Set union() Method [![Image 3: Python3 Lists](#) Python Sets](#) * * * ## Description The `union()` method returns the union of two sets, which includes all elements from both sets, with duplicate elements appearing only once. ## Syntax The syntax for the `union()` method is: set.union(set1, set2...) ## Parameters * set1 -- Required, the target set to merge with. * set2 -- Optional, other sets to merge. Multiple sets can be provided, separated by commas `,`. ## Return Value Returns a new set. ## Example Merging two sets, with duplicate elements appearing only once: ## Example 1 x = {"apple", "banana", "cherry"} y = {"google", "tutorial", "apple"} z = x.union(y)print(z) The output is: {'cherry', 'tutorial', 'google', 'banana', 'apple'} Merging multiple sets: ## Example 1 x = {"a", "b", "c"} y = {"f", "d", "a"} z = {"c", "d", "e"} result = x.union(y, z)print(result) The output is: {'c', 'd', 'f', 'e', 'b', 'a'} [![Image 4: Python3 Lists](#) Python Sets](#)
← Vue ExamplesRef Set Symmetric_Difference β†’