Python – Convert Matrix to Sets of Set
In this article, the task is to write Python program to Convert Matrix to sets of set.
Examples:
Input : test_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Output : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Explanation : Converted to sets of frozensets to remain immutable and hashable.
Input : test_list = [[5, 6, 3], [1, 7, 9]]
Output : {frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Explanation : Converted to sets of frozensets to remain immutable and hashable.
Method #1 : Using set() + frozenset() + generator expression
In this, we perform iteration till Matrix in generator expression to get inner sets, and outer container is converted to set using set(). Inner container needs to be hashable, hence has to be frozenset() for creation.
Python3
# Python3 code to demonstrate working of# Convert Matrix to Sets of Set# Using set() + frozenset() + generator expression# initializing listtest_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]# printing original listprint("The original list is : " + str(test_list))# frozenset() to get inner elements hashable required for set()res = set(frozenset(sub) for sub in test_list)# printing resultprint("Converted set Matrix : " + str(res)) |
Output:
The original list is : [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Converted set Matrix : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}
Method #2 : Using map() + frozenset() + set()
In this, map() is used to extend logic of converting inner containers to frozenset().
Python3
# Python3 code to demonstrate working of# Convert Matrix to Sets of Set# Using map() + frozenset() + set()# initializing listtest_list = [[5, 6, 3], [1, 7, 9], [8, 2, 0]]# printing original listprint("The original list is : " + str(test_list))# map() to extend logic to inner elementsres = set(map(frozenset, test_list))# printing resultprint("Converted set Matrix : " + str(res)) |
Output:
The original list is : [[5, 6, 3], [1, 7, 9], [8, 2, 0]]
Converted set Matrix : {frozenset({8, 0, 2}), frozenset({1, 9, 7}), frozenset({3, 5, 6})}


