# 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.

Assume we have taken an input set. We will now break this input set into a list of sets element-wise using the below-mentioned methods.

### 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

#### Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

• Create a function listOfSets() that breaks the input set into a list of sets element-wise by accepting the input set as an argument.

• Creating an empty list for storing the resultant list of sets.

• Use the for loop to traverse through each element of the input set.

• Use the set() function to create an empty set.

• Use the add() function to add the current element of an input set to the above-created empty set by passing the current element as an argument to it.

• Use the append() function(adds the element to the list at the end) to append the above empty set(containing an element of the set) to the output list.

• Return the resultant list of sets using the return statement.

• Create a variable to store an input set.

• Call the above-defined listOfSets() function by passing the input Set as an argument to it to print the resultant list of sets.

#### Example

The following program breaks an input set into a list of sets using the for loop and append() function −

# creating a function that breaks the input set
# into a list of sets element-wise by accepting the input set as an argument
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
# 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:\n", listOfSets(inputSet))


#### Output

On execution, the above program will generate the following output −

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

### Lambda Function

Lambda Function, often known as an 'Anonymous Function,' is the same as a normal Python function except that it can be defined without a name. The def keyword is used to define normal functions, while the lambda keyword is used to define anonymous functions. They are, however, limited to a single line of expression. They, like regular functions, can accept several parameters.

#### Syntax

lambda arguments: expression

• This function accepts any number of inputs but only evaluates and returns one expression.

• Lambda functions can be used wherever function objects are necessary.

• You must remember that lambda functions are syntactically limited to a single expression.

### Map() Function

The map() function in python returns a map object(iterator) of the output after applying the specified function to each item of a specified iterable like a list, tuple, etc.

#### Syntax

map(function, iterable)


#### Parameters

• Function − a function to which a map passes every element of a specified iterable.

• Iterable − an iterable that is to be mapped.

#### Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

• Create a variable to store an input set.

• Access all the values of the set using the lambda function and change the value to set using the {} operator.

• The map/apply this condition to all elements of the set using the map() function.

• Convert this set of sets to a list of sets using the list() function.

• Print the resultant list of sets.

#### Example

The following program breaks an input set into a list of sets using map() and lambda functions −

# 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:\n", listOfSets)


#### Output

On execution, the above program will generate the following output −

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

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

#### Example

The following program breaks an input set into a list of sets using list comprehension −

# 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 list of sets:\n", listOfSets)


#### Output

On execution, the above program will generate the following output −

The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
[{'tutorialspoint'}, {'python'}, {'hello'}]


## Conclusion

In this article, we have learned how to break the given set into a list of sets using 3 different approaches. We also learned how to apply a specific condition to iterable elements using the lambda function and how to apply this condition to all elements of the iterable using the map() function