set-data-structure

Python Set Data Structure Practical Example

Last Updated | Dec, 29, 2020 By Kingsley Ijomah
In this tutorial, you'll learn about Python's Set data structure. You'll look at some of the best scenarios to use it and finally, we will visit the set data structure's methods with sample codes.

A set is an unordered collection with no duplicate elements, although mutable, and can have different types (integer, tuple, string etc). It cannot contain mutable elements such as lists, or dictionaries as its elements.

Sets can be used for mathematical set operations such as union, intersection, difference and symmetric difference.

This article is a continuation of Python Data Structures For Beginners

Let us explore set in more details... and in the end, solve a problem using set data structure.

Topics covered in this post:

1. Creating Python Sets
2. Modifying a set in Python
3. Removing elements from a set
4. Set Union Operations
5. Set Intersection Operations
6. Set Difference Operations
7. Set Symmetric Difference
8. Python Set Methods
9. Set Code Challenge

Creating Python Sets

code
# Different of set types in Python

# integers
set = {1, 2, 3}

# mixed data types
set = {3.2, "Hi", (1, 2, 3)}

# Distinguish set and dictionary while creating an empty set

# initialize a with {}
empty = {}
print(type(empty)) # Output => <class 'dict'>

# initialize a with set()
empty = set()
print(type(empty)) # Output => <class 'set'>

Modifying a set in Python

Sets are mutable and also unordered and therefore cannot be indexed, we cannot apply usual indexing or slicing when it comes to modifying set.

Single items are added with the add( ) method and multiple items added with the update( ) method. The update method can accept, list, tuple, dict. or string

code
set_example = {'hello', 'world'}

# you cannot call using indexing
# TypeError: 'set' object is not subscriptable
set_example[0]

# using add() method
# output => {'hello', 'yay!', 'world'}
set_example.add('yay!')
print(set_example)

# using update() method
# add multiple elements
# using different data types
# output => {1, 2, 'yay!', 3, 4, 5, 6, 7, 8, 'world', 'hello'}
set_example.update([1,2,3],{4,5,6},(7,8,))
print(set_example)

Removing elements from a set

You can remove items from set using either discard( ) or remove( ) methods.

discard( ) will not raise an error if the item is not present in the set whilst remove( ) will raise an error.

code
set_example = {1, 3, 4, 5}


# discard an element
# output => {1, 4, 5}
set_example.discard(3)
print(set_example)

# discard element not present
# no errors raised
set_example.discard(30)


# remove an element not present
# KeyError: 30
set_example.remove(30)

We can also remove items using pop() and clear() methods.

Since set is unordered using pop() there is no way of knowing which element will be removed

code
set_example = {1, 3, 4, 5, 6}

# pop an element
# output => random element
set_example.pop()
print(set_example)

# pop another element
# output => random element
set_example.pop()
print(set_example)

# clear the set
# output => set()
set_example.clear()
print(set_example)

Set Union Operations

Union of two sets is a union that contains all elements of both sets. Union is performed with either | operator or union( ) operator

code
# Set union

a = {1, 2, 3}
b = {4, 5, 6}


# using | operator
# output => {1, 2, 3, 4, 5, 6}
print(a | b)


# use union () function
# output => {1, 2, 3, 4, 5, 6}
print(a.union(b))

Set Intersection Operations

Intersection between two sets returns the elements the sets have in common.

It is performed using the & operator or the intersection() method

code
# Set Intersection

a = {1, 2, 3, 6, 7}
b = {4, 5, 6, 7, 8, 9}

# using & operator
# output => {6, 7}
print(a & b)

# use the intersection operator
# output => {6, 7}
print(a.intersection(b))

Set Difference Operations

Set difference of (a - b) is a set with elements found in (a) but not in (b)

This is achieved either with the - operator or the difference() method.

code
# Set Difference

a = {1, 2, 3, 6, 7, 9}
b = {4, 5, 6, 7, 8, 9}

# using - operator
# output => {1, 2, 3}
print(a - b)

# using - operator
# output => {8, 4, 5}
print(b - a)


# using difference() function
# output => {1, 2, 3}
print(a.difference(b))

Set Symmetric Difference

Symmetric difference of (a) and (b) are the elements found in (a) that are missing in (b) plus all elements found in (b) missing in (a)

This is achieved using ^ operator and the symmetric_difference()

code
# Set Symmetric Difference

a = {1, 2, 3, 6, 7, 9}
b = {4, 5, 6, 7, 8, 9}

# using ^ operator
# output => {1, 2, 3, 4, 5, 8}
print(a ^ b)

# using symmetric_difference method
# output => {1, 2, 3, 4, 5, 8}
print(a.symmetric_difference(b))

Python Set Methods

add() - Adds an element to the set
clear() - Removes all the elements from the set
copy() - Returns a copy of the set
difference() - Returns a set containing the difference between sets
difference_update() - Removes the items in this set that are also in another
discard() - Remove the specified item
intersection() - Returns a set, that is the intersection of two other sets
intersection_update() - Removes items in a set that are not present in other
isdisjoint() - Returns whether two sets have a intersection or not
issubset() - Returns whether another set contains this set or not
issuperset() - Returns whether this set contains another set or not
pop() - Removes an element from the set
remove() - Removes the specified element
symmetric_difference() - Returns a set with the symmetric differences
symmetric_difference_update() - inserts the symmetric differences
union() - Return a set containing the union of sets
update() - Update the set with the union of this set and others

Set Code Challenge

Let's look at an example where the set data structure could be used.

Isogram Example

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however, spaces and hyphens are allowed to appear multiple times.

Solution Steps

  • Create a method that accepts a string as an argument
  • loop through each character of the string
  • extract only alphabet letters from the string
  • make all lower case to be the same
  • pass through set() to filter out duplicates
  • see if the length of the string has changed ( duplicates found)
  • if changed return False (not isogram) otherwise return True (it is isogram)

Time to see some code in action...

code
def is_isogram(string):

    # return if alphabet letter
    # make returned letter a lower case
    # e.g: ['a','n','g','o','l','a']
    string = [x.lower() for x in string if x.isalpha()]

    # set will remove duplicates and return e.g ['n','g','o','l']
    # len of set will not match len of original string
    # and so will return False
    return len(set(string)) == len(string)

# non repeating letters
is_isogram('lumberjacks') # Output => True

# repeating letters
is_isogram('Angola') # Output => False

Conclusion

Facts about the set data structure it is unordered and mutable but yet can only contain immutable types, it cannot contain duplicates making it ready handy when we have to work on a situation where uniqueness is important.

Now when a problem you are trying to solve meets any of the above facts about set() then you know how to go about tackling it.

MY STORY

My name is Kingsley Ijomah, I am the founder of CODEHANCE, an online education platform built with you in mind, a place where I express my gratitude to a skill ( coding ) which has changed my life completely.

Learn To Code With Me.
FREE course included.

GET IN TOUCH

Enter your email address:

Delivered by FeedBurner