Union of Set in PythonSet:The set type built-in Python includes the following features:
A Set in Python is an unsorted collection of distinct elements. You can use 'curly braces{}' or the built-in 'set() method' to build a set. Code: Sets can be used for a number of different actions, including adding and removing elements, determining whether an element is a part of a set, locating the union or intersection of sets, and more. These are a few instances: Code: Union of Set:The set union operation in Python is used to combine two sets into a single set that contains all the unique elements from both sets. The union() method or the '|' operator are used to conduct the union operation. In this article, we will discuss how to perform set union in Python using both these methods. Using the '|' operator:The '|' operator is used to perform the set union operation in Python. It accepts two sets as operands and outputs a new set that is entirely composed of the elements from the input sets, with no duplicates. Syntax: Example: Union of Set Using '|' Operator. Output: In the above example, we have two sets set1 and set2, and we perform the set union operation using the '|' operator. Without any duplications, the final set contains every element from both sets. Using the union() method:The set union operation in Python is carried out using the union() function. It accepts one or more sets as parameters and outputs a new set that contains each set's distinct items. Syntax: Example: Union of Set Using union() Method. Output: In the above example, we have two sets set1 and set2, and we perform the set union operation using the union() method. There are no duplicate elements in the final set, only the unique components from both sets. We can also perform the set union operation on more than two sets using the union() method. Example: Let's examine how to union three sets using the set method. Output: In the above example, we have three sets set1, set2, and set3, and we perform the set union operation using the union() method. Each unique element from each of the three sets is present in the final set, which is duplicate-free. Using the update() method:The update() method is used to add all the elements from one set to another set. This method can also be used to perform the set union operation in Python. Syntax: Example: Using the update() method. Output: In the above example, we have two sets set1 and set2, and we perform the set union operation using the update() method. Without any duplications, the final set contains every element from both sets. Conclusion:To combine two or more sets into a single set that contains all of the distinct members from all of the sets, utilise Python's set union operator. The union operation can be performed using the '|' operator, the union() method, or the update() method. All of the elements from all of the sets are combined into the final set without any duplications. Next TopicUnit Testing in Django |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India