standard-library-in-x

Notes and readings for STL workshop

View on GitHub

Sets

Introduction

Creating a set

add()

update()

discard() and remove()

copy()

Using assignment here instead of copy() will create a pointer to the already existing set.

clear()

pop()

difference, intersection, union

isdisjoint, issubset, issuperset

symmetric_difference()

sorting a set

One example where sets might be used is when we want to count the number of distinct numbers in a given list.

l = [1,2,5,4,2,3,1,5]
s=set(l)
print(len(l)) #number of distinct elements

Output:

5