02. Sets

Sets are an unordered collection of items with no duplicates. If you add an element that already exists within a set, nothing happens.

Creating sets

To create a set, we may use the setfunction, or use curly braces ({}).

set() function

With the set function, we can grab the unique characters from within a string.

>>> yellowFruit = set('banana') 
>>> yellowFruit 
{'b', 'a', 'n'}

Curly braces {}

If we create sets using curly braces, then we can be more specific as to which strings we want in our set.

>>> banana = {'ba', 'an', 'na'}
>>> banana
{'ba', 'an', 'na'}

Frozen set

In a regular set, the elements can be modified with methods add() and remove(). Due to this, we cannot use a hash values within regular sets, causing a slight decrease in efficiency.

Instead, we may use frozen sets to achieve hashability. These are immutable, but are slightly more efficient. To call a frozen set, use the frozenset function to create one.

Set functions

You may compare sets as they are mathematically.

x in s
Does x exist in s?
x not in s
Does x not exist in s?
Union. Returns set with elements from both s and t.
s & t
Intersection. Returns set of elements common to s and t.
s - t
Returns a new set with elements in s but not in t.
Copies a new set with a shallow copy of s.
Disjoint set. Boolean returns whether set s and c have anything in common.
s <= t, s < t
Subset. Checks if s is a subset of t.
s >= t, s > t
Check if s is a superset of t.

Here are some samples of the functions above.

>>> s = set('banana')
{'b', 'a', 'n'}
>>> print(s)
>>> t = set('pineapple')
{'a', 'i', 'e', 'n', 'l', 'p'}
>>> print(t)
>>> len(s)
>>> s.union(t)
{'a', 'i', 'e', 'b', 'n', 'l', 'p'}
>>> s.intersection(t)
{'a', 'n'}
>>> s.difference(t)

Set Manipulation

s |= t
Update s by adding elements in t.
s &= t
Intersection updating.
Updates s to keep elements that are found in both s and t.
s -= t
Different updating.
Updates s to keep only the elements that are in s but not in t.
s ^= t
Symmetric difference.
Updates s to keep only the elements that are either s or t.
Adds item to s.
Remove item from s.
Removes item from s.

Learn to be a Pythonista!

Learn Python in One Day

Learn to be a Pythonista! Try Python

Have you always wanted to learn computer programming but are afraid it'll be too difficult for you? Or you're familiar with some programming but are interested in learning Python fast? Then this book is for you. You no longer have to waste your time and money learning Python from lengthy books, expensive online courses or complicated Python tutorials.

$ Check price
11.9711.97Amazon 4 logo(185+ reviews)

More Python resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources