Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Breaking a Set into a list of sets using Python
In this article, we will learn how to break a Set into a list of sets in Python, where each element of the original set becomes a separate single-element set.
A set is an unordered collection of unique elements. Sometimes we need to convert each element into its own set within a list structure. We'll explore three different approaches to accomplish this task ?
Methods Used
The following are the various methods used to accomplish this task ?
Using For Loop and append(), add() functions
Using Python map() & lambda functions
Using List Comprehension
Method 1: Using For Loop and append(), add() Functions
This method iterates through each element of the input set and creates a new set containing only that element ?
def listOfSets(inputSet):
# creating an empty list for storing a resultant list of sets
outputList = []
# traversing through each element of the set
for k in inputSet:
# creating an empty set using set()
emptySet = set()
# adding the current element of the input set to the above empty set
emptySet.add(k)
# appending empty set to the outputList
outputList.append(emptySet)
# returning the resultant list of sets
return outputList
# input set
inputSet = {'hello', 'tutorialspoint', 'python'}
# Printing the given set
print("The given set is:", inputSet)
# calling the above listOfSets() function by passing
# the inputSet to it to print the resultant list of sets
print("Breaking the input set into a list of sets:")
print(listOfSets(inputSet))
The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
[{'tutorialspoint'}, {'python'}, {'hello'}]
Method 2: Using Python map() & Lambda Functions
The map() function applies a given function to each item of an iterable. Combined with a lambda function, we can create single-element sets efficiently ?
Syntax
map(function, iterable) lambda arguments: expression
# input set
inputSet = {'hello', 'tutorialspoint', 'python'}
# Printing the given set
print("The given set is:", inputSet)
# Modify every element of the set to a separate set using the {} operator.
# Applying this condition to all the elements of the set using a map()
# Converting this result to a list
listOfSets = list(map(lambda k: {k}, inputSet))
# printing the resultant list of sets
print("Breaking the input set into a list of sets:")
print(listOfSets)
The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
[{'tutorialspoint'}, {'python'}, {'hello'}]
Method 3: Using List Comprehension
List comprehension provides a concise way to create lists based on existing iterables. This is the most Pythonic approach ?
# input set
inputSet = {'hello', 'tutorialspoint', 'python'}
# Printing the given set
print("The given set is:", inputSet)
# Traversing through every element of the set and
# converting it to a separate set using the {} operator
listOfSets = [{i} for i in inputSet]
# printing the resultant list of sets
print("Breaking the input set into a list of sets:")
print(listOfSets)
The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
[{'tutorialspoint'}, {'python'}, {'hello'}]
Comparison
| Method | Code Lines | Readability | Performance |
|---|---|---|---|
| For Loop | More | Good | Slower |
| map() + lambda | Medium | Medium | Fast |
| List Comprehension | Least | Excellent | Fastest |
Conclusion
We explored three methods to break a set into a list of sets. List comprehension is the most efficient and Pythonic approach, while the for loop method provides better readability for beginners. Choose the method that best fits your coding style and requirements.
