You are currently viewing Python set Operators

In this article, we will discuss different operators that are available in the Set data structure in Python. Python Sets is a one-dimensional, and unordered data structure that will not allow duplicates.

Advertisements

Let’s look at some of the examples of Python set operators:

1. Quick examples for Python Set Operators.


# Union
print("id U cost - ",id.union(cost))
print("id | cost - ",id|cost)

# Intersection
print("id ∩ cost - ",id.intersection(cost))
print("id & cost - ",id & cost)

# Symmetric Difference
print("id Δ cost - ",id.symmetric_difference(cost))
print("id ^ cost - ",id ^ cost)

# Difference
print("id - cost - ",id.difference(cost))
print("id - cost - ",id - cost)

# Output:
# id U cost -  {34, 11, 12, 45, 21, 23, 56}
# id | cost -  {34, 11, 12, 45, 21, 23, 56}
# id ∩ cost -  {34, 11, 21}
# id & cost -  {34, 11, 21}
# id Δ cost -  {23, 56, 12, 45}
# id ^ cost -  {23, 56, 12, 45}
# id -cost -  {56, 12}
# id - cost -  {56, 12}

2. Set union(|) operator

Union will return only the unique elements from two/multiple sets. union() method is used to get unique elements. It can also be possible to use | operator to perform union functionality.

2.1 Union Syntax:

Let’s see the syntax of union() and | operator.


# Syntax for union using method
set1.union(set2,set3,...)
# Syntax for union using operator
set1|set2|set3|...

2.2 Union Example

Let’s create 4 sets and perform union operation using union() method and | operator.


# Create four sets
id={12,34,56,21,11}
cost={23,45,11,21,34}
item1={"cake","oil","water","petrol","gas"}
item2={"cake","oil"}

# Actual elements in the sets.
print("Set-1",id)
print("Set-2",cost)
print("Set-3",item1)
print("Set-4",item2)

# Output:
# Set-1 {34, 11, 12, 21, 56}
# Set-2 {34, 11, 45, 21, 23}
# Set-3 {'petrol', 'oil', 'cake', 'gas', 'water'}
# Set-4 {'oil', 'cake'}

Output:


# perform union on id and cost using union()
print("id U cost - ",id.union(cost))
print("item1 U item2 - ",item1.union(item2))

# perform union on id and cost using | operator
print("id | cost - ",id|cost)
print("item1 | item2 - ",item1|item2)

# Output:
# id U cost -  {34, 11, 12, 45, 21, 23, 56}
# item1 U item2 -  {'oil', 'cake', 'gas', 'petrol', 'water'}
# id | cost -  {34, 11, 12, 45, 21, 23, 56}
# item1 | item2 -  {'oil', 'cake', 'gas', 'petrol', 'water'}

Explanation:

In the above code, we created four sets and performing union on

  1. id and cost sets using union() method and | operator. So the unique elements from both the sets are: {34, 11, 12, 45, 21, 23, 56}.
  2. item1 and item2 sets using union() method and | operator. So the unique elements from both the sets are: {‘cake’, ‘gas’, ‘oil’, ‘water’, ‘petrol’}.

3. Set intersection(&) operator

Intersection will return only the common elements present in two/multiple sets. intersection() method is used to get elements that are in common. It can also be possible to use & operator to perform the intersection functionality.

3.1 Intersection Syntax

Let’s see the syntax of intersection() and ‘&’ operator.


# Syntax for intersection using method
set1.intersection (set2,set3,...)
# Syntax for intersection using operator
set1&set2&set3&...

3.2 Intersection Example

Let’s create 4 sets and perform intersection operation using intersection() method and ‘&’ operator.


# Create four sets
id={12,34,56,21,11}
cost={23,45,11,21,34}
item1={"cake","oil","water","petrol","gas"}
item2={"cake","oil"}

# Actual elements in the sets.
print("Set-1",id)
print("Set-2",cost)
print("Set-3",item1)
print("Set-4",item2)

# Output:
# Set-1 {34, 11, 12, 21, 56}
# Set-2 {34, 11, 45, 21, 23}
# Set-3 {'petrol', 'oil', 'cake', 'gas', 'water'}
# Set-4 {'oil', 'cake'}

Output:


# perform intersection on id and cost using intersection()
print("id ∩ cost - ",id.intersection(cost))
print("item1 ∩ item2 - ",item1.intersection(item2))

# perform intersection on id and cost using & operator
print("id & cost - ",id & cost)
print("item1 & item2 - ",item1 & item2)

# Output
# id ∩ cost -  {34, 11, 21}
# item1 ∩ item2 -  {'cake', 'oil'}
# id & cost -  {34, 11, 21}
# item1 & item2 -  {'cake', 'oil'}

Explanation:

In the above code, we created four sets and performing intersection on

  1. id and cost sets using intersection() method and ‘&’ operator. So the common elements from both the sets are: {34, 11, 21}.
  2. item1 and item2 sets using intersection() method and ‘&’ operator. So the common elements from both the sets are: {‘cake’, ‘oil’}.

4. Set symmetric-difference(^) operator

As we seen that intersection will return only the common elements present in two/multiple sets. Symmetric Difference is vice-versa. symmetric_difference() method is used to get elements that are not in common. It can also be possible to use ^ operator to perform the symmetric-difference functionality.

4.1 Syntax for symmetric_difference and ^ operator

Let’s see the syntax for symmetric_difference and ^ operator.


# Syntax for symmetric-difference using method
set1.symmetric_difference(set2,set3,...)
# Syntax for symmetric-difference using operator
set1^set2^set3^...

4.2 Symmetric Difference Example

Let’s create 4 sets and perform symmetric-difference operation using symmetric_difference() method and ‘^’ operator.


# Create four sets
id={12,34,56,21,11}
cost={23,45,11,21,34}
item1={"cake","oil","water","petrol","gas"}
item2={"cake","oil"}

# Actual elements in the sets.
print("Set-1",id)
print("Set-2",cost)
print("Set-3",item1)
print("Set-4",item2)

# Output:
# Set-1 {34, 11, 12, 21, 56}
# Set-2 {34, 11, 45, 21, 23}
# Set-3 {'petrol', 'oil', 'cake', 'gas', 'water'}
# Set-4 {'oil', 'cake'}

Output:


# perform symmetric_difference on id and cost using symmetric_difference()
print("id Δ cost - ",id.symmetric_difference(cost))
print("item1 Δ item2 - ",item1.symmetric_difference(item2))

# perform symmetric_difference on id and cost using ^ operator
print("id ^ cost - ",id^cost)
print("item1 ^ item2 - ",item1^item2)

# Output
# id Δ cost -  {23, 56, 12, 45}
# item1 Δ item2 -  {'water', 'gas', 'petrol'}
# id ^ cost -  {23, 56, 12, 45}
# item1 ^ item2 -  {'water', 'gas', 'petrol'}

Explanation:

In the above code, we created four sets and performing symmetric-difference on

  1. id and cost sets using symmetric_difference() method and ‘^’ operator. So the elements that are not common in both the sets are: {23, 56, 12, 45}.
  2. item1 and item2 sets using symmetric_difference() method and ‘^’ operator. So the elements that are not common in both the sets are: {‘gas’, ‘water’, ‘petrol’}.

5. Set difference(-) operator

Difference will return all the elements from the first set such that all these elements are not present in any of the sets. difference() method is used to get elements from the first set such that all these elements are not present in any of the sets. It can also be possible to use – (minus) operator to perform the set-difference functionality.

5.1 Set difference() and – operator Syntax

Let’s see the syntax for difference() and – operator.


# Syntax for difference using method
set1.difference(set2,set3,...)
# Syntax for difference using operator
set1-set2-set3-...

5.2 Example for difference and – operator

Let’s create 4 sets and perform difference operation using difference() method and ‘-‘ operator.


# Create four sets
id={12,34,56,21,11}
cost={23,45,11,21,34}
item1={"cake","oil","water","petrol","gas"}
item2={"cake","oil"}

# Actual elements in the sets.
print("Set-1",id)
print("Set-2",cost)
print("Set-3",item1)
print("Set-4",item2)

# Output:
# Set-1 {34, 11, 12, 21, 56}
# Set-2 {34, 11, 45, 21, 23}
# Set-3 {'petrol', 'oil', 'cake', 'gas', 'water'}
# Set-4 {'oil', 'cake'}

Output:


# perform difference on id and cost using difference()
print("Difference of id and cost - ",id.difference(cost))
print("Difference of item1 and item2 - ",item1.difference(item2))

# perform difference on id and cost using - operator
print("id - cost - ",id-cost)
print("item1 - item2 - ",item1-item2)

# Output
# Difference of id and cost -  {56, 12}
# Difference of item1 and item2 -  {'water', 'gas', 'petrol'}
# id - cost -  {56, 12}
# item1 - item2 -  {'water', 'gas', 'petrol'}

Explanation:

In the above code, we created four sets and performing difference on

  1. id and cost sets using difference() method and ‘-‘ operator. So the elements present in id but not in cost are: {56, 12}.
  2. item1 and item2 sets using difference() method and ‘-‘ operator. So the elements present in item1 but not in item2 are: {‘gas’, ‘water’, ‘petrol’}.

6. Difference between method and operator

MethodOperator
union(),intersection().symmetric_difference() and difference() are methodsU,∩,Δ,- are the operators
These methods explicitly fix the priority of the setsoperators will have specific operator precedence of the sets
Methods are used only with Sets.operators can be used with other data structures

7. Conclusion

In this article, we discussed the set operators performed on the sets with actual methods. Both methods and operators will perform the same functionality. But internally, operator performance is based on it’s precedence.