Topics Covered
Helpful links
- Full Advanced Python Course Link
- Gitlab Code Page
- Additional Help at Python.org
- Google Colab: The Easiest Way to Code
What is a python set?
A set is a class created in Python to hold distinct elements within the same collection. You can create a set using the set() method, which converts any iterable into a sequence of iterables with different elements, commonly known as set.
Parameters: A list, tuple, or other sequences.
Returns: If no element is supplied, the result is an empty set.
The Difference Between Set and Lists
A list is ordered and can contain duplicate elements, a set on the other hand cannot contain duplicate elements and is unordered.
Sets also have numerous functions to allow you to identify duplicate, different, or symetrically different elements with the set. Very useful stuff.
How do you write a set in Python?
All the components (elements) in a set are enclosed within curly braces “{} “, separated by a comma, or with the built-in set() function. It might contain any number of items, and they can be of various sorts (integer, float, tuple, string, etc.).
An empty set is created by either using curly braces or passing no parameter to the built-in function.
mySet = set([1, 2, 3])
print(mySet)
{1, 2, 3}
Python Set Examples
empty1 = { } # make an empty set with no elements
empty2 = set() # another way to create an empty set, without any items in it
my_set1 = { 1, 3 } # set with 2 elements and it is iterable
my_set2 = set([1, 2]) # another way to create a set from a list of elements
snow_men = { 'snowman', 'yeti', 'abominable-snowman' }
snow_men_set = set(snow_men) # a set from a list of words
employee_set1 = { 'John', 'Mary', 'Peter' } # another subset with 3 elements, this time not iterable
employee_set2 = set([ 'Mary', 'Peter' ]) # subset with 2 elements
How do you sort a set in Python?
The simplest way to sort a set in Python is by using the sorted() function like this:
my_set1 = { 1, 3 }
sorted_set = sorted(my_set1) # iterable sets will be converted to a list, and they will be sorted in ascending order
print(sorted_set)
[1, 3]
How do you know if a set is empty?
There is a simple check for this.
empty_set = set()
if empty_set:
print('Set not empty')
else:
print('Set empty')
Set empty
How do you add/insert an element in a set?
You can use the add() function to add values
employee_set1 = { 'John', 'Mary', 'Peter' }
employee_set1.add('Joe')
print(employee_set1)
{'John', 'Joe', 'Peter', 'Mary'}
How do you remove an element from Set?
You can remove one item at a time with remove() function.
snow_men = set({ 'snowman', 'yeti', 'abominable-snowman' })
snow_men.remove('yeti')
print(snow_men)
{'abominable-snowman', 'snowman'}
Using the Set Intersection Method to Identify Shared Items Between Sets
The intersection() method produces a new set with items that are shared by all sets.
employee_set1 = { 'John', 'Mary', 'Peter' }
employee_set2 = set([ 'Mary', 'Peter' ])
print(employee_set1.intersection(employee_set2))
{'Peter', 'Mary'}
Using the Set Differnce Method to Identify Differnet Items Between Sets
The difference() method does the opposite of the intersection method and returns items that do not exist in the other set.
employee_set1 = { 'John', 'Mary', 'Peter' }
employee_set2 = set([ 'Mary', 'Peter' ])
print(employee_set1.difference(employee_set2))
{'John'}
Using the Set Symetrical Differnence Method
The symetrical_diffence method will find the outer differences in both sets. Here is an example:
x = {1,2,3}
y = {3,4,5}
z = x.symmetric_difference(y)
print(z)
{1, 2, 4, 5}