Python: Set [Data Type]

Sets in Python are collections of items, which hold unique items.

A Python Set is unordered collection of Zero or more hashable unique object references.

Items in a set do not have a fixed index position, so a Set can not be sliced or strided.

Set Capabilities

Let’s check the capabilities of Python Set-

Set can do

Contain unique items
Can be changed(mutable) after creation
Contain only hashable items

Set can not do

Can not contain duplicate items
Maintain order of the items
Cano not contain mutable data types, like – list, set, etc.

WARNING

Define a Set

We can define a Set using “set” function. Check the example below-

big_box_set = set()

print("Type: ", type(big_box_set))
print("Length: ", len(big_box_set))


# Add items using add()
big_box_set.add("one")
big_box_set.add("two")
big_box_set.add("three")

print(big_box_set)
print("Length: ", len(big_box_set))

print(dir(big_box_set))
Python

Output:

Type:  <class 'set'>

Length:  0

{'three', 'one', 'two'}

Length:  3


['__and__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
Plaintext

Or we can use curly braces to define a Set-

big_box_set = {"First item", "Second item", "Third item", 4, 5, 100, "Last Item here"}

print("Type: ", type(big_box_set))
print("Length: ", len(big_box_set))

print(big_box_set)

empty_set = {}

print(empty_set)
Python

Output:

Type:  <class 'set'>

Length:  7

{100, 4, 5, 'Third item', 'Second item', 'Last Item here', 'First item'}

{}
Plaintext

“set()” Constructor

Let’s discuss the “set()” constructor in detail, as we need to be aware of the behavior of this, else we might get unexpected result.

set() accepts one parameter, and converts that to a Python set. If we pass a string, then it splits the string character into each element and saves those in a set.

big_box_set = set("bigboxcode")

print(big_box_set)
print(type(big_box_set))
print(len(big_box_set))
Python

Output:

{'c', 'e', 'o', 'i', 'g', 'x', 'b', 'd'}

<class 'set'>

8
Plaintext

The parameter passed to set() needs to be an iterable. If we pass a value that is not iterable, then it throws an error-

big_box_set = set(100)
Python

Output:

Traceback (most recent call last):
  File "set_example.py", line 1, in <module>
    big_box_set = set(100)
                  ^^^^^^^^
TypeError: 'int' object is not iterable
Plaintext

We can pass any iterable to the set() constructor and a Set will be derived from that iterable. Check the example below, where we have passed a tuple and list to the set() constructor-

big_box_set = set((100, "abc", 99.99, "big", "box", "code", 100, "box"))

print(big_box_set)
print(type(big_box_set))
print(len(big_box_set))

second_set = set([100, "abc", 99.99, "big", "box", "code", 100, "box"])

print(second_set)
print(type(second_set))
print(len(second_set))
Python

Output:

{99.99, 100, 'box', 'big', 'code', 'abc'}
<class 'set'>
6

{99.99, 100, 'box', 'big', 'code', 'abc'}
<class 'set'>
6
Plaintext

Hashable Elements Only

A Python set only contains hashable items. If the item is not hashable, and we try to add that to a Set, then we get an “unhashable type” error.

Check the code below. Here we are trying to add a list to Set. Check what error we are getting-

big_box_set = {100, "abc", 99.99, "big", "box", "code", 100, 99.00, "box"}

big_box_set.add(['a', 'b', 'c'])
Python

Output:

Traceback (most recent call last):
  File "set_example.py", line 3, in <module>
    big_box_set.add(['a', 'b', 'c'])
TypeError: unhashable type: 'list'
Plaintext

Set Union

We can perform union operations on two(or more) sets. Union operation will combine all items of those sets.

There are several ways to perform a union between sets-

  • Use the operator “|” to perform the union operation.
  • Use function “union” on the first set and pass the second set as param.
  • Use the “update” function, but in that case the first set will be updated with the union result.
big_box_set = {100, "abc", 99.99, "big", "box", "code"}
second_set = {100, "big", "box", "code", 200, "unique"}

# Union using |
set_union_result_1 = big_box_set | second_set

print(set_union_result_1)

# Union using union function
set_union_result_2 = big_box_set.union(second_set)

print(set_union_result_2)

# Union and update the existing(first) set
big_box_set.update(second_set)

print(big_box_set)

# The same union and update can be performed using |=
first_set = {100, 200, 'abc'}
second_set = {500, 100, 'abc', 99.99}

first_set |= second_set

print(first_set)
Python

Output:

{'unique', 99.99, 100, 200, 'box', 'big', 'code', 'abc'}
{'unique', 99.99, 100, 200, 'box', 'big', 'code', 'abc'}
{'unique', 99.99, 100, 200, 'box', 'big', 'code', 'abc'}
{99.99, 100, 500, 200, 'abc'}
Plaintext

Set Intersection

Use the “&” operator to get the intersection between two(or more) sets. This will give us items which common for those sets.

first_set = {100, "abc", 99.99, "big", "box", "code"}
second_set = {100, "big", "box", "code", 200, "unique"}

# Intersect using &
set_intersect_result_1 = first_set & second_set

print(set_intersect_result_1)

# Intersection using intersection function
# Similar to using &
set_intersect_result_2 = first_set.intersection(second_set)

print(set_intersect_result_2)

# Perform intersection and update the first set
first_set.intersection_update(second_set)

print(first_set)

# The same intersection and update can be performed using &=
first_set = {100, 200, 'abc'}
second_set = {500, 100, 'abc', 99.99}

first_set &= second_set

print(first_set)
Python

Output:

{'big', 100, 'box', 'code'}
{'big', 100, 'box', 'code'}
{'big', 100, 'box', 'code'}
{'abc', 100}
Plaintext

Set Difference

Use the minus(-) sign to get the difference between two sets. But this will give items which exists in the first set but not in the second one.

big_box_set = {100, "abc", 99.99, "big", "box", "code"}
second_set = {100, "big", "box", "code", "unique"}

set_diff_result = big_box_set - second_set

print(set_diff_result)
Python

Output:

{99.99, 'abc'}
Plaintext

Use the carate(^) sign to get the full difference between 2 sets-

first_set = {100, "abc", 99.99, "big", "box", "code"}
second_set = {100, "big", "box", "code", "unique"}

set_diff_result_1 = first_set ^ second_set

print(set_diff_result_1)

# Perform symmentic difference using symentic_difference function
set_diff_result_2 = first_set.symmetric_difference(second_set)

# Perform difference and also update the first
first_set.symmetric_difference_update(second_set)

print(first_set)

# The same symentic difference can be performed using ^= operator
first_set = {100, 200, "abc"}
second_set = {500, 100, "abc", 99.99}

first_set ^= second_set

print(first_set)
Python

Output:

{99.99, 'abc', 'unique'}
{99.99, 'abc', 'unique'}
{200, 99.99, 500}
Plaintext

Frozen Set

There is a variation of set in Python, which can not be changed once created. So the frozen set is immutable.

Use “frozenset” function to create a frozen set. Pass an iterable to the “frozenset” function.

big_box_set = frozenset((100, "abc", 99.99, "big", "box", "code", 100, 99.00, "box"))

print(big_box_set)
print(type(big_box_set))
print(len(big_box_set))
Python

Output:

frozenset({99.99, 100, 'code', 99.0, 'big', 'box', 'abc'})
<class 'frozenset'>
7
Plaintext

Though we can not add or delete items to/from a frozen set, once it is set, but we can perform some other set operation. Check the examples below, for some common operations on set-

big_box_set = frozenset((100, "abc", 99.99, "big", "box", "code", 100, 99.00, "box"))

print("Check existance result: ", 100 in big_box_set)
print("Copy result: ", big_box_set.copy())
print("Intersection result: ", big_box_set.intersection({100, 200, "big", "box"}))
Python

Output:

Check existance result:  True
Copy result:  frozenset({99.99, 100, 99.0, 'abc', 'big', 'code', 'box'})
Intersection result:  frozenset({'box', 100, 'big'})
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.