Sets#

Programming for Geoscientists Data Science and Machine Learning for Geoscientists

Sets are a type of Python container that are equivalent to sets in maths. They can only contain one copy of a particular element and they are not ordered. They don’t have indices or keys. Elements in a set can be of any type.

Sets are defined with curly brackets:

    set1 = {variable1, variable2, variable3, etc...}

Sets only contain one copy of the same element, so even though we specify it twice, a set stores it once:

set1 = {1, 4, 5, 2, 3, 1}
print(set1)
{1, 2, 3, 4, 5}

We can see that the values are returned in no particular order.

Sets cannot be added together with simple addition method like lists can. They cannot be sliced or indexed. Operations that can be done on a set:

set2 = {"potato", "banana", "carrot", 4, 5}

set3 = set2 - set1 # Remove elements from set2 that are in set1
print(set3)

print(4 in set2)  # Check if 4 is in set2

print(set2.add("cucumber")) # Add an element to set
print(set2)

set4 = set() # Create an empty set
print(set4)
{'carrot', 'banana', 'potato'}
True
None
{4, 5, 'carrot', 'cucumber', 'banana', 'potato'}
set()

Exercises#


  • Define a function which returns how many unique values there are in a list.

HINT: use set()


  • Define a function which calculates intersection of sets in a given list of sets.

HINT: Use the intersection() function