Open In App

Python set operations (union, intersection, difference and symmetric difference)

Last Updated : 18 Dec, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

This article demonstrates different operations on Python sets.
Examples:

Input :
A = {0, 2, 4, 6, 8}
B = {1, 2, 3, 4, 5}

Output :
 Union : [0, 1, 2, 3, 4, 5, 6, 8]
 Intersection : [2, 4]
 Difference : [8, 0, 6]
 Symmetric difference : [0, 1, 3, 5, 6, 8]

In Python, below quick operands can be used for different operations.

| for union.
& for intersection.
– for difference
^ for symmetric difference




# Program to perform different set operations
# as we do in  mathematics
  
# sets are define
A = {0, 2, 4, 6, 8};
B = {1, 2, 3, 4, 5};
  
# union
print("Union :", A | B)
  
# intersection
print("Intersection :", A & B)
  
# difference
print("Difference :", A - B)
  
# symmetric difference
print("Symmetric difference :", A ^ B)


Output:

('Union :', set([0, 1, 2, 3, 4, 5, 6, 8]))
('Intersection :', set([2, 4]))
('Difference :', set([8, 0, 6]))
('Symmetric difference :', set([0, 1, 3, 5, 6, 8]))

Previous Article
Next Article

Similar Reads

Python - Symmetric Difference of Dictionaries
Given two Dictionaries, the task is to write a Python program to get the symmetric difference. Examples: Input : test_dict1 = {'Gfg' : 4, 'is' : 3, 'best' : 7, 'for' : 3, 'geek' : 4}, test_dict2 = {'Gfg' : 4, 'is' : 3, 'good' : 7, 'for' : 3, 'all' : 4} Output : {'all': 4, 'good': 7, 'best': 7, 'geek': 4} Explanation : all, good, best and geek are m
6 min read
Python - Symmetric Difference of Multiple sets
Symmetric Differences among groups of sets are elements that belong to any one of the sets but are not present in any other set. Given a list of sets and the task is to write a Python program to get the symmetric difference of the same. Input : test_list = [{5, 3, 2, 6, 1}, {7, 5, 3, 8, 2}, {9, 3}, {0, 3, 6, 7}] Output : {8, 1, 9, 0} Explanation :
3 min read
Python | Find Symmetric Pairs in dictionary
Sometimes, while working with Python dictionary, one can have a problem in which one desires to get key-value pairs that are symmetrical, i.e that has key-value pair of same value irrespective of the fact value is a key or value. Let's discuss certain ways in which this task can be performed. Method #1 : Using generator + loop This task can be solv
6 min read
Python - Extract Symmetric Tuples
Sometimes while working with Python tuples, we can have a problem in which we need to extract all the pairs which are symmetric, i.e for any (x, y), we have (y, x) pair present. This kind of problem can have application in domains such as day-day programming and web development. Let's discuss certain ways in which this task can be performed. Input
4 min read
Fernet (symmetric encryption) using Cryptography module in Python
Cryptography is the practice of securing useful information while transmitting from one computer to another or storing data on a computer. Cryptography deals with the encryption of plaintext into ciphertext and decryption of ciphertext into plaintext. Python supports a cryptography package that helps us encrypt and decrypt data. The fernet module o
3 min read
Python Program to check if a matrix is symmetric
A square matrix is said to be a symmetric matrix if the transpose of the matrix is the same as the given matrix. The symmetric matrix can be obtained by changing row to column and column to row. Examples: Input : 1 2 3 2 1 4 3 4 3Output : Yes Input : 3 5 8 3 4 7 8 5 3Output : No Method 1: A Simple solution is to do the following. 1) Create a transp
3 min read
Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
Given two strings, find if we can make first string from second by deleting some characters from second and rearranging remaining characters. Examples: Input : s1 = ABHISHEKsinGH : s2 = gfhfBHkooIHnfndSHEKsiAnG Output : Possible Input : s1 = Hello : s2 = dnaKfhelddf Output : Not Possible Input : s1 = GeeksforGeeks : s2 = rteksfoGrdsskGeggehes Outpu
2 min read
Set update() in Python to do union of n arrays
We are given n arrays of any size which may have common elements, we need to combine all these arrays in such a way that each element should occurs only once and elements should be in sorted order? Examples: Input : arr = [[1, 2, 2, 4, 3, 6], [5, 1, 3, 4], [9, 5, 7, 1], [2, 4, 1, 3]] Output : [1, 2, 3, 4, 5, 6, 7, 9] A simple solution for this prob
2 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg