Open In App

Python – Symmetric Difference of Multiple sets

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 8, 1, 9, 0 occur just 1 time over whole container. 

Input : test_list = [{5, 3, 2, 6, 1}, {7, 5, 3, 8, 2}, {9, 3}]

Output : {8, 1, 9}

Explanation : 8, 1, 9 occur just 1 time over whole container. 

Method #1: Using Counter() + chain.from_iterable() 

This method is used to check for all elements that have 1 as frequency overall sets by flattening. Counter() extracts frequencies and then all elements with count 1 can be extracted. 

Python3




# Python3 code to demonstrate working of
# Symmetric Difference of Multiple sets
# Using Counter() + chain.from_iterable()
from collections import Counter
from itertools import chain
 
# initializing list
test_list = [{5, 3, 2, 6, 1},
             {7, 5, 3, 8, 2},
             {9, 3},
             {0, 3, 6, 7}]
              
# printing original list
print("The original list is : " + str(test_list))
 
# getting frequencies using Counter()
# from_iterable() flattens the list
freq = Counter(chain.from_iterable(test_list))
 
# getting frequency count 1
res = {idx for idx in freq if freq[idx] == 1}
 
# printing result
print("Symmetric difference of multiple list : " + str(res))


Output:

The original list is : [{1, 2, 3, 5, 6}, {2, 3, 5, 7, 8}, {9, 3}, {0, 3, 6, 7}]

Symmetric difference of multiple list : {8, 1, 9, 0}

Method #2 : Using Counter() + chain.from_iterable() + items()

Similar to above method, only difference being its performed in single step by extracting keys and values using items().

Python3




# Python3 code to demonstrate working of
# Symmetric Difference of Multiple sets
# Using Counter() + chain.from_iterable() + items()
from collections import Counter
from itertools import chain
 
# initializing list
test_list = [{5, 3, 2, 6, 1},
             {7, 5, 3, 8, 2},
             {9, 3}, {0, 3, 6, 7}]
              
# printing original list
print("The original list is : " + str(test_list))
 
# clubbing operations using items() to get items
res = {key for key, val in Counter(chain.
                                   from_iterable(test_list)).
       items() if val == 1}
 
# printing result
print("Symmetric difference of multiple list : " + str(res))


Output:

The original list is : [{1, 2, 3, 5, 6}, {2, 3, 5, 7, 8}, {9, 3}, {0, 3, 6, 7}]

Symmetric difference of multiple list : {8, 1, 9, 0}



Previous Article
Next Article

Similar Reads

Python Program to Find Duplicate sets in list of sets
Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result. Input : test_list = [{4, 5, 6, 9}, {6, 4, 1, 5}, {
8 min read
Python set operations (union, intersection, difference and symmetric difference)
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 interse
1 min read
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 - Find union of multiple sets
Given multiple sets list, the task is to write a Python program to find union of each set. Examples: Input : test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}] Output : {1, 2, 3, 4, 5, 7, 8, 9} Explanation : All elements from all sets included. Duplicates removed. Input : test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}] Outp
5 min read
Multiple Sets Intersection in Python
In this article, a List of sets is given our task is to write a program to perform their intersection using Python. Examples of finding the intersection of multiple setsInput : test_list = [{5, 3, 6, 7}, {1, 3, 5, 2}, {7, 3, 8, 5}, {8, 4, 5, 3}] Output : {3, 5} Explanation : 3 and 5 is present in all the sets.Method 1: Using a Logical operator This
2 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
Sets in Python
A Set in Python programming is an unordered collection data type that is iterable, mutable and has no duplicate elements. Set are represented by { } (values enclosed in curly braces) The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. Thi
7 min read
Practice Tags :