How To Create A Set Of Sets In Python?
Last Updated :
12 Feb, 2024
Sets are flexible data structures in Python that hold distinct items. There are situations in which you may need to construct a set of sets, even though sets are unordered and flexible in and of themselves. In this article, we will see how to create a set of sets.
Note: We can't create a set of sets in Python as sets are mutable and not hashable preventing nesting within sets.
Create a Set of Sets in Python
Below are some of the ways by which we can Create a Set of Set In Python.
- Using Frozensets
- Using Set Comprehension
Create a Set of Sets Using Frozensets
The below approach code creates a set named `set_of_sets` containing three frozensets: `set1`, `set2`, and `set3`. Each frozenset is created directly using the `frozenset()` constructor. These frozensets are then added to the set `set_of_sets`, ensuring uniqueness. The `print(set_of_sets)` displays the frozensets it contains.
Python3
# using frozenset
set1 = frozenset({1, 2, 3})
set2 = frozenset({4, 5, 6})
set3 = frozenset({7, 8, 9})
set_of_sets = {set1, set2, set3}
#printing output
print(type(set_of_sets))
print(set_of_sets)
Output<class 'set'>
{frozenset({1, 2, 3}), frozenset({4, 5, 6}), frozenset({8, 9, 7})}
Create a Set of Sets Using Set Comprehension
The below approach uses set comprehension to generate a set of sets, where each inner set is a frozenset containing a range of consecutive integers in increments of 3. The resulting set_of_sets is displayed along with its type.
Python3
# using set comprehension
set_of_sets = {frozenset(range(i, i+3)) for i in range(1, 10, 3)}
#printing result
print(type(set_of_sets))
print(set_of_sets)
Output<class 'set'>
{frozenset({1, 2, 3}), frozenset({4, 5, 6}), frozenset({8, 9, 7})}
Conclusion:
In conclusion, constructing a set of sets in Python can be achieved through various methods, such as using a list of sets, frozensets, or set comprehension, each offering distinct advantages based on the specific requirements of your application. These approaches showcase the flexibility of sets in accommodating diverse data structures. Overall, the choice of method depends on the desired characteristics and functionality for the set of sets in your Python program.
Similar Reads
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 hasha
5 min read
Creating a List of Sets in Python Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python.Using List ComprehensionList comprehen
2 min read
Creating Sets of Tuples in Python Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read
Find the size of a Set in Python A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Pythonâs set class represents the mathematical notion of a set. The size of a set means the amount of memory (in bytes) occupied by a set object. In this article, we will learn various ways to get th
2 min read
Get a Subset of Dict in Python In Python, dictionaries store key-value pairs and are pivotal in data management. Extracting dictionary subsets based on specific criteria is key for targeted data analysis, ensuring operational efficiency by focusing on relevant data. This technique, vital in data processing and machine learning, a
3 min read