Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Set Methods (union(), intersection(), difference(), etc.) in Python


In Python, sets are powerful collections that allow you to perform a variety of mathematical operations such as union, intersection, and difference. These operations are essential when working with sets, and Python provides built-in methods to perform these operations. This article explores some common set methods, including union(), intersection(), and difference(), along with examples.

1. union() Method

The union() method returns a new set that contains all the unique elements from two or more sets. This method effectively combines the sets by including every element from each set, removing duplicates automatically.

Example: Using union()

    # Union of two sets
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    result = set1.union(set2)
    print(result)  # Outputs: {1, 2, 3, 4, 5}
        

In this example, the union of set1 and set2 includes all the elements from both sets, with duplicates removed.

2. intersection() Method

The intersection() method returns a new set containing only the elements that are present in both sets. If there are no common elements, the result will be an empty set.

Example: Using intersection()

    # Intersection of two sets
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    result = set1.intersection(set2)
    print(result)  # Outputs: {3}
        

In this example, the intersection of set1 and set2 returns the element 3 as it is the only common element between the two sets.

3. difference() Method

The difference() method returns a new set that contains the elements that are in the first set but not in the second set. If there are no unique elements, the result will be an empty set.

Example: Using difference()

    # Difference of two sets
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    result = set1.difference(set2)
    print(result)  # Outputs: {1, 2}
        

In this example, the difference of set1 and set2 returns the elements 1 and 2 that are in set1 but not in set2.

4. symmetric_difference() Method

The symmetric_difference() method returns a new set that contains the elements that are in either of the sets, but not in both. It is essentially the union of the differences of both sets.

Example: Using symmetric_difference()

    # Symmetric difference of two sets
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    result = set1.symmetric_difference(set2)
    print(result)  # Outputs: {1, 2, 4, 5}
        

In this example, the symmetric difference of set1 and set2 returns the elements that are only in one of the sets, excluding the common elements 3.

5. issubset() and issuperset() Methods

These methods are used to compare sets and check if one set is a subset or superset of another.

Example: Using issubset() and issuperset()

    # Checking if a set is a subset or superset
    set1 = {1, 2, 3}
    set2 = {1, 2, 3, 4, 5}
    set3 = {4, 5}

    print(set1.issubset(set2))  # Outputs: True (set1 is a subset of set2)
    print(set2.issuperset(set1))  # Outputs: True (set2 is a superset of set1)
    print(set2.issuperset(set3))  # Outputs: True (set2 is a superset of set3)
        

In this example, set1 is a subset of set2, and set2 is a superset of both set1 and set3.

6. isdisjoint() Method

The isdisjoint() method checks if two sets have no elements in common. If the sets do not share any elements, it returns True, otherwise it returns False.

Example: Using isdisjoint()

    # Checking if two sets are disjoint
    set1 = {1, 2, 3}
    set2 = {4, 5, 6}
    set3 = {3, 4, 5}

    print(set1.isdisjoint(set2))  # Outputs: True (set1 and set2 are disjoint)
    print(set1.isdisjoint(set3))  # Outputs: False (set1 and set3 are not disjoint)
        

In this example, set1 and set2 have no elements in common, so they are disjoint. On the other hand, set1 and set3 share the element 3, so they are not disjoint.

7. Conclusion

Python sets provide a variety of useful methods for performing mathematical set operations such as union, intersection, difference, and symmetric difference. Additionally, methods like issubset(), issuperset(), and isdisjoint() allow you to compare sets and check their relationships. By mastering these set methods, you can efficiently work with unique collections of data in Python.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java