set#

Array of unique values. You can create it by using {<element1>, <element2>, ..., <elementn>} or tuple function.

In the following example, I show that it can contain only unique elements. Although element 2 is specified twice, it appears only once as a result.

{1,2,3,4,2}
{1, 2, 3, 4}

Empty set#

To create an empty set you should:

  • Use the set function without arguments;

  • Do not use empty curly brackets, it will create empty dict not set;

  • Do not use curly curly brackets with a comma (lite it works for tuple), it will cause an error.

The following cell shows that curly brackets alone create a tuple but not a set. And the set() expression will in turn create an instance of set.

print(type({}))
print(type(set()))
<class 'dict'>
<class 'set'>

The following cell shows that using braces with a comma is an error.

{,}
  Cell In[3], line 1
    {,}
     ^
SyntaxError: invalid syntax

Interestingly, applying the print function to an empty set results in set() output.

print(set())
set()

No order#

One of the main features of the set datatype is that it doesn’t have any order. This has important implications:

  • You can’t apply indexing syntax (the [] operator) to sets;

  • There are no functions associated with indexing (e.g. pop in the list datatype).

Only immutable#

In sets, you can only use immutable datatypes. The following cells show that if you add a mutable type to a tuple (e.g. list), you will get an error.

{1,"1", 9.4, (1,3,4)}
{(1, 3, 4), 1, '1', 9.4}
{1,"1", 9.4, (1,3,4), [3,4,5]}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 {1,"1", 9.4, (1,3,4), [3,4,5]}

TypeError: unhashable type: 'list'

add new value#

test_set = {1,2,3}
print("initital set", test_set)
test_set.add("s")
print("result set", test_set)
initital set {1, 2, 3}
result set {1, 2, 3, 's'}

Note that adding an element that already exists in the set won’t cause an error, it will simply be ignored.

test_set = {1,2,3,4}
print("initial set", test_set)
test_set.add(3)
print("restult set", test_set)
initial set {1, 2, 3, 4}
restult set {1, 2, 3, 4}

remove delete by value#

You can delete element by value.

test_set = {1,2,3,4,5}
print("initial list", test_set)
test_set.remove(4)
print("resutl list", test_set)
initial list {1, 2, 3, 4, 5}
resutl list {1, 2, 3, 5}

Note that if there’s no removing value in the source list, you’ll have an error.

test_set = {5,3,4,1}
test_set.remove(6)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[10], line 2
      1 test_set = {5,3,4,1}
----> 2 test_set.remove(6)

KeyError: 6

intercection#

This method allows you to get a set that contains only common elements for argument sets.

test_set1 = {1,2,3}
test_set2 = {2,3,4}
test_set1.intersection(test_set2)
{2, 3}

union#

This method allows you to get a set that contains all elements from both argument sets.

test_set1 = {1,2,3}
test_set2 = {2,3,4}
test_set1.union(test_set2)
{1, 2, 3, 4}

symmetric_difference#

This method allows you to get a set that contains elements that are present in only one of the argument sets, but not in both.

test_set1 = {1,2,3}
test_set2 = {2,3,4}
test_set1.symmetric_difference(test_set2)
{1, 4}

Sets difference A-B#

In case you subtract set A from set B you get a set of elements from the set not contained in set B.

test_set1 = {1,2,3}
test_set2 = {2,3,4}
test_set1 - test_set2
{1}