Creating a List of Sets in Python
Last Updated :
27 Dec, 2024
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 Comprehension
List comprehension creates a list of sets faster than loops by avoiding extra operations. It generates sets with consecutive integers using a compact syntax and the specified range.
Example:
Python
# Create a list of sets, each containing 3 consecutive numbers
a = [set(range(i, i + 3)) for i in range(0, 10, 3)]
print(a) # Set list
Output[{0, 1, 2}, {3, 4, 5}, {8, 6, 7}, {9, 10, 11}]
Explanation:
- This creates a list of sets with three consecutive integers each.
- It iterates from 0 to 9 with a step of 3, forming sets .
Using map()
map()
function is used to apply a specific function to each element of an iterable. It is efficient for transforming or processing data into sets, especially for large datasets.
Example:
Python
# Create a list of ranges, each containing 3 consecutive numbers
a = [range(i, i + 3) for i in range(0, 10, 3)]
# Convert each range in the list to a set using map
res = list(map(set, a))
print(res)
Output[{0, 1, 2}, {3, 4, 5}, {8, 6, 7}, {9, 10, 11}]
Explanation:
- Creates a list of ranges with three consecutive integers.
- Converts each range in
a
to set. - Prints the list of sets.
Using For loop
For loop manually creates and appends sets to list, allowing for additional logic during set creation. It is slightly less efficient due to intermediate operations like append()
.
Example:
Python
# Initialize an empty list to store sets
a = []
# Loop through numbers from 0 to 9 with a step of 3
for i in range(0, 10, 3):
# Create a set containing three consecutive numbers starting from 'i'
b = set(range(i, i + 3))
# Append the set to the list 'a'
a.append(b)
print(a)
Output[{0, 1, 2}, {3, 4, 5}, {8, 6, 7}, {9, 10, 11}]
Explanation:
- Initializes an empty list for sets.
- Loops through range with steps of 3.
- Creates and appends set of three integers to
a
.
Similar Reads
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul
3 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
Python - Create List of Size n Creating a list of size n in Python can be done in several ways. The easiest and most efficient way to create a list of size n is by multiplying a list. This method works well if we want to fill the list with a default value, like 0 or None.Python# Size of the list n = 5 # Creating a list of size n
2 min read
Make a Set of Lists in Python Creating a set of lists typically involves defining multiple lists and organizing them into a structure, such as a dictionary or another list. In this article, we will see how we can make a set of lists in Python.Table of ContentCreating a Set of List Using list() FunctionMake A Set Of Lists Using D
3 min read