10+ simple examples to learn python sets in detail

In this tutorial, we will learn about Python sets, which are unique data structures with interesting properties.

 

What are Python sets

  • Sets share the same characteristics of lists and dictionaries.
  • A set is a collection of information like a list; however, like a key in a dictionary, sets can only contain unique values.
  • They are also an unordered collection.
  • This means that they cannot be accessed by index but rather by the value itself like dictionary keys.
  • They can be iterated through though, like how dictionary keys can be looped over.
  • Sets are practical in situations of storing unique items.

 

Comparison between different Python Data Types

Below table shows a summary of the differences between each data types:

Data TypeOrderedIterableUniqueImmutableMutable
ListYesYesNoNoYes
DictionaryNoYesKeys onlyKeys onlyValues only
TupleYesYesNoYesNo
SetNoYesYesNoYes
FrozensetNoYesYesYesNo

 

Creating python sets

Method-1: Using set()

The first method to create a set is to use the in-built set() function. The function takes either an iterable (like a list or a tuple) or a sequence (lists, tuples, and strings are all sequences).

#!/usr/bin/env python3
# Creating sets

mylist = set(['dec', 'abd', 'bip']) # Convert from list to set
mystring = set('Python set') # Convert from string to set
mydict = set({'val1': 1, 'val2': 2}) # Convert from dictionary to set
mytuple = set((1,2,3,4)) # Convert from tuple to set

print(mylist)
print(mystring)
print(mydict)
print(mytuple)

In this example I am converting different Python datatypes using set() function. Few things to NOTE in the output:

  • For list the content is changed based on the alphabetical order which shows that the content of set is not fixed and can change so unordered.
  • For string, individual character is stripped into individual item of set
  •  For dictionary only the key is printed and not the value from the key value pair.
  • In case of tuple, the content almost the same. The only difference is that the bracelets are changed to curly braces.

Output from this script:

~]# python3 create-set.py
{'abd', 'dec', 'bip'}
{'P', 'n', ' ', 't', 's', 'o', 'e', 'h', 'y'}
{'val1', 'val2'}
{1, 2, 3, 4}

 

Method-2: Using curly brackets

Since we know that the syntax of python set is items within curly braces so we can use curl brackets to create sets. I have used different type of examples in this example python script:

#!/usr/bin/env python3

set1 = { 'dec', 'abd', 'bip', 'abd' }
set2 = { 'Python set' }
set3 = {1,2,3,4}

print(set1)
print(set2)
print(set3)

Output from this script:

~]# python3 create-set.py
{'dec', 'bip', 'abd'}
{'Python set'}
{1, 2, 3, 4}

 

Add items to python sets

Example-1: Using set.add()

There are two methods to add items to sets, in this example we will use set.add() function. With set.add() you can only add one item at a time.
10+ simple examples to learn python sets in detail

Output from this script:

~]# python3 update-set.py
{'cd', 'ad', 'ef', 'ab'}

If you try to add more than one item then you will get TypeError:

 ~]# python3 update-set.py
Traceback (most recent call last):
  File "update-set.py", line 5, in 
    set2 = set1.add('cd', 'ad')
TypeError: add() takes exactly one argument (2 given)

 

Example-2: Using set.update()

If we have to add multiple items or a list of values to existing set then we can use update.set(). In this python script example I am adding items directly by using set.update() within curly braces and in the same script I will create a new set and append it to set1.
10+ simple examples to learn python sets in detail

Output from this script:

~]# python3 update-set.py
{'ef', 'ad', 'bc', 'cd', 'ab'}
{'ef', 'ad', 'bc', 'cd', '12', '9i', 'bg', 'ab'}

 

Access item from Python sets

Example-1: Using for loop

Since python set is iterable, we can use for loop to iterate over individual items of the python set.

#!/usr/bin/env python3

set1 = {'apple', 'mango', 'grapes'}

for val in set1:
    print(val)

Output from this script:

  ~]# python3 access-set.py
apple
mango
grapes

 

Example-2: Using "in" operator

We can use in operator to check for the presence of an item inside set. This will return boolean response i.e. True or False:

#!/usr/bin/env python3

set1 = {'apple', 'mango', 'grapes'}

if 'mango' in set1:
    print('mango found in set1')
else:
    print('mango not found in set1')

Output from this script:

 ~]# python3 access-set.py
mango found in set1

 

Remove key from Python set

Method-1: Using pop()

We have pop() in-built function which is used to remove and return an item from the set. Items are removed from the beginning of the set

#!/usr/bin/env python3

set1 = {'ab', 'dc', 'cd'}

set1.pop()
print(set1)

We I execute the script multiple times, then the removed item is always different:

 ~]# python3 remove-item-sets.py
{'cd', 'dc'}

 ~]# python3 remove-item-sets.py
{'dc', 'cd'}

 ~]# python3 remove-item-sets.py
{'ab', 'dc'}

 ~]# python3 remove-item-sets.py
{'ab', 'cd'}

 

Method-2: Using remove()

We can use remove() function to remove a single item from the set but not print it on the console. So we can silently remove any item from the Python set using remove()

#!/usr/bin/env python3

set1 = {'ab', 'dc', 'cd'}

set1.remove('dc')
print(set1)

Output from this script:

 ~]# python3 remove-item-sets.py
{'ab', 'cd'}

But if the provided key doesn't exist in the set then you will get KeyError

 ~]# python3 remove-item-sets.py
Traceback (most recent call last):
  File "remove-item-sets.py", line 5, in 
    set1.remove('da')
KeyError: 'da'

 

Method-3: Using discard()

You can also use the discard() method. For comparison, discard() does not raise a KeyError if the item to be discarded does not exist.

#!/usr/bin/env python3

set1 = {'ab', 'dc', 'cd'}

set1.discard('da')
print(set1)

In this example, the provided key which I am trying to remove using discard() doesn't exist, so let's check the output:

~]# python3 remove-item-sets.py
{'dc', 'cd', 'ab'}

So we don't get any errors on the console this time. To remove a positive content from the python set:

#!/usr/bin/env python3

set1 = {'ab', 'dc', 'cd'}

set1.discard('ab')
print(set1)

Output from this script:

 ~]# python3 remove-item-sets.py
{'cd', 'dc'}

 

Method-4: Remove all the items from Python set

We can use clear() method to remove all of the data from the set object:

#!/usr/bin/env python3

set1 = {'ab', 'dc', 'cd'}

set1.clear()
print(set1)

Output from this script:

 ~]# python3 remove-item-sets.py
set()

 

Python set operations

In particular, Python sets implement all the fundamental operations from set theory, like union, intersection, subset tests etc. With them, we can express algorithms in a more declarative way, avoiding lots of nested loops and conditionals

FunctionSyntaxDescription
a.add(x)N/AAdd element x to the set a
a.clear()N/AReset the set a to an empty state, discarding all of its elements
a.remove(x)N/ARemove element x from the set a
a.discard(x)N/AThis is similar to a.remove(x) but will not throw KeyError if x is not part of the a set
a.pop()N/ARemove an arbitrary element from the set a, raising KeyError if the set is empty
a.union(b)a | bAll of the unique elements in a and b
a.update(b)a |= bSet the contents of a to be the union of the elements in a and b
a.intersection(b)a & bAll of the elements in both a and b
a.intersection_update(b)a &= bSet the contents of a to be the intersection of the elements in a and b
a.difference(b)a - bThe elements in a that are not in b
a.difference_update(b)a -= bSet a to the elements in a that are not in b
a.symmetric_difference(b)a ^ bAll of the elements in either a or b but not both
a.symmetric_difference_update(b)a ^= bSet a to contain the elements in either a or b but not both
a.issubset(b)<=True if the elements of a are all contained in b
a.issuperset(b)>=True if the elements of b are all contained in a
a.isdisjoint(b)N/ATrue if a and b have no elements in common

 

Set Union

10+ simple examples to learn python sets in detail
Python set operations - union

A union between sets is the set of all items/elements in both sets.

To achieve union between sets in Python, we can use the union method, which is defined on set objects:
10+ simple examples to learn python sets in detail

Output from this script:

 ~]# python3 set-union.py
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}

So there are three methods we can get union of two sets and all of them give us the same result.

 

Set intersection

10+ simple examples to learn python sets in detail
Python set operations - intersection

An intersection of sets is the set of all items that appear in all of the sets, that is, what they have in common. The & operator or the intersection method can be used:

Let us have a script with some common and different data:
10+ simple examples to learn python sets in detail

Output from this script:

 ~]# python3 set-intersection.py
{3, 4, 5}
{3, 4, 5}
{3, 4, 5}

So we have the unique keys which are part of both the sets.

 

Set difference

The difference between two sets is basically what is in one set and not in the other.

10+ simple examples to learn python sets in detail
Python set operations - difference

In this sample script we have two different sets and we will get the difference between both the sets under different scenarios:
10+ simple examples to learn python sets in detail

Output from this script:

~]# python3 set-difference.py
{1, 2}
{6, 7, 8, 9, 10}
{1, 2}
{6, 7, 8, 9, 10}

 

Python subsets using issubset()

We have inbuilt function issubset() which can be used to check whether all of one set's elements exist in another set (that is, whether the set is a subset of another):

#!/usr/bin/env python3

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

print(a.issubset(b))
print(b.issubset(a))

For example, in this script we know that all the keys from set a is part of set b but let's check this programmatically:

~]# python3 python-subset.py
True
False

So the first condition has returned True while the second condition is False

 

Compare the keys of different sets

We can use comparison operator to check if the sets contain same or different keys. It is not important that keys are in the same order as long as all the keys are present, for example here I have two sets defined with set of keys but in a different order and we will use if condition to check the content of both the sets:
10+ simple examples to learn python sets in detail

Output from this script:

 ~]# python3 set-union.py
Both sets have same keys

Similarly we can use != operator to check if both sets have different keys.

 

Frozensets

Frozensets are essentially the combination of a set and a tuple. They are immutable, unordered, and unique. These are perfect for sensitive information like bank account numbers, as you wouldn’t want to alter those. They can be iterated over, but not indexed.

There is no special syntax to represent frozenset literals—they must be created by calling the constructor. The standard string representation in Python 3 looks like a frozenset constructor call.

#!/usr/bin/env python3

print(frozenset(range(10)))

Output from this script:

 ~]# python3 set-union.py
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})

 

Conclusion

In this tutorial we learned about Python sets and performing different kind of operation with set data type with examples. Now se know that sets are collections of unique and unordered items. We covered operations that you can perform on sets, such as finding unions and intersections, and other specialized operations, such as finding the difference and symmetric difference. We also looked at what frozen sets are, and the potential uses for them.

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.