An Introduction to Python Sets. Part I: Defining set objects in Python

An Introduction to Python Sets.

Part I: Defining set objects in PythonChaitanya BawejaBlockedUnblockFollowFollowingJan 31You 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:How to define set objects in PythonDiscover some of the operations supported natively.

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 SetThere are two ways to define a set in Python:built-in set() functionwith curly braces {}Using set functionThe 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:Resulting set is unorderedDuplicates are removedThe 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 bracesAlternately, 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 setAs 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)}>>> xset([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.

ConclusionPython’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 LinksWhat is a set?Python SetsIf you like this story, you might also like my story on List Comprehension in Python and Functions as Objects in Python.

.. More details

Leave a Reply