An Introduction to Python Sets - Part I

Defining set objects in Python

Chaitanya Baweja
Feb 1 · 3 min read

You must have come across sets and set theory in your high school education. It’s the same class where you used to draw these cool overlapping circles called Venn diagrams.

A Venn Diagram (Source : realpython.com [link])

While a rigorous definition of a set can be abstract, a set can be simply defined as an unordered collection of items. Every element in a set is unique (no duplicates) and a variety of operations like union, intersection etc. can be performed on a set.

Grouping objects into a set can be useful in programming as well, and Python provides a built-in set type to do so. What you’ll learn in this tutorial:

  1. How to define set objects in Python
  2. Discover some of the operations supported natively.
  3. Set operations in detail and get a understanding as to when a set is an appropriate choice.

This tutorial has been divided into three parts for ease of access. Link to tutorial for part II and III will be added soon.

Defining a Set

There are two ways to define a set in Python:

  • built-in set() function
  • with curly braces {}

Using set function

The set function takes in as argument an iterable like a list or tuple. The syntax is as follows:

output_set = set(<iterable>)

For example:

Two important things to notice in the output above:

  1. Resulting set is unordered
  2. Duplicates are removed

The 2nd property by which duplicates are removed allows us to use set objects for various operations like indexing etc.

Sets are significantly faster when it comes to determining if an object is present in the set, but are slower than lists when it comes to iterating over their contents.

Like lists and tuples, strings can be passed to set() function as well.

Using curly braces

Alternately, a set can be defined with curly braces ({}). The syntax is as follows:

x = {<item>, <item>, ..., <item>}

Each <item> will become a distinct element of the set, even if item is iterable. Let’s see how this works in practice:

The difference in the two methods lies in how they deal with iterables.

>>> {'abbbac'}
{'abbbac'}
>>> set('abbbac')
{'a', 'c', 'b'}

Define an empty set
As Python interprets empty curly braces as an empty dictionary, we define an empty set in Python as:

Python does not require that sets contain similar objects. The elements in a set can be objects of different types:

>>> x = {2, 2.01, 'abc', (1,2,3)}
>>> x
set([2, 'abc', 2.01, (1, 2, 3)])

An important property of sets to understand is that while set itself can be modified, elements in the set must be immutable. So, while a tuple can be a part of a set, a list cannot.

Conclusion

Python’s built-in set type has the following characteristics:

Sets are unordered.

Set elements are unique. Duplicate elements are not allowed.

A set itself may be modified, but the elements contained in the set must be of an immutable type.

Helpful Links

  1. What is a set?
  2. Python Sets

Part II of this story is available here.
Part III of this story is available here.

If you like this story, you might also like my story on List Comprehension in Python and Functions as Objects in Python.

Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Chaitanya Baweja

Written by

Machine Learning Engineer | Python | Data Analytics | Economics | Physics

Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade