Python Return Set From Function

5/5 - (1 vote)

Do you need to create a function that returns a set but you don’t know how? No worries, in sixty seconds, you’ll know! Go! ?

A Python function can return any object such as a set. To return a set, first create the set object within the function body, assign it to a variable your_set, and return it to the caller of the function using the keyword operation “return your_set“.

For example, the following code creates a function create_set() that iterates over all numbers 0, 1, 2, …, 9, adds them to the set your_set, and returns the set to the caller of the function:

def create_set():
    ''' Function to return set '''
    your_set = set()
    for i in range(10):

    return your_set

numbers = create_set()
# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Note that you store the resulting set in the variable numbers. The local variable your_set that you created within the function body is only visible within the function but not outside of it. So, if you try to access the name your_set, Python will raise a NameError:

>>> print(your_set)
Traceback (most recent call last):
  File "", line 9, in <module>
NameError: name 'your_set' is not defined

To fix this, simply assign the return value of the function — a set — to a new variable and access the content of this new variable:

>>> numbers = create_set()
>>> print(numbers)
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

There are many other ways to return a set from a function in Python. For example, you can use a set comprehension statement instead that is much more concise than the previous code—but creates the same set of numbers:

def create_set():
    ''' Function to return set '''
    return {i for i in range(10)}

numbers = create_set()
# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Set comprehension is a very useful Python feature that allows you to dynamically create a set by using the syntax {expression context}. You iterate over all elements in a given context “for i in range(10)“, and apply a certain expression, e.g., the identity expression i, before adding the resulting values to the newly-created set.

In case you need to learn more about set comprehension, feel free to check out my explainer video:

Python Set Comprehension -- A Simple Formula Guide

Related Article: A Simple Introduction to Set Comprehension in Python

An interesting way to return a set from a function is to use lambda functions.

A lambda function is an anonymous function in Python. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. Use the set() constructor to create and return a set object.

The following code snippet uses a combination of features.

  • The lambda function dynamically creates a function object and assigns it to the variable create_set. You can then call the function like before with create_set().
  • The set comprehension expression creates a set and return it at the same time in a single line of code—it cannot get more concise than that.
create_set = lambda : {i for i in range(10)}

numbers = create_set()
# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

πŸš€ If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skillsβ€”even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar β€œHow to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, tooβ€”from the comfort of your own home.

Join the free webinar now!