PYTHON SETS
DATA STRUCTURE AND ALGORITHM
What is SET?
● Python’s built-in set type has the following characteristics:
○ Sets are unordered.
○ Set elements are unique. Duplicate elements are not
allowed.
○ A set itself may be modified, but the elements contained
in the set must be of an immutable type.
Set Items, Unordered, Unchangeable
● Set Items
○ Set items are unordered, unchangeable, and do not allow
duplicate values.
● Unordered
○ Unordered means that the items in a set do not have a defined
order.
○ Set items can appear in a different order every time you use
them, and cannot be referred to by index or key.
● Unchangeable
○ Set items are unchangeable, meaning that we cannot change
the items after the set has been created.
Duplicates Not Allowed
● Sets cannot have two items with the same value.
Get the Length of a Set
● To determine how many items a set has, use the len() function.
Set Items - Data Types
● Set items can be of any data type:
type()
● Set items can be of any data type:
○ <class 'set'>
The set() Constructor
● It is also possible to use the set() constructor to make a set.
Python - Access Set Items
● You cannot access items in a set by referring to an index or a key.
● But you can loop through the set items using a for loop, or ask if a
specified value is present in a set, by using the in keyword.
● Loop through the set, and print the values:
Example
● Check if "banana" is present in the set:
Change Items
● Once a set is created, you cannot change its items, but you can add
new items.
Python - Add Set Items
● Add Items
● Once a set is created, you cannot change its items, but you can add
new items.
● To add one item to a set use the add() method.
Add Sets
● To add items from another set into the current set, use the
update() method.
Add Any Iterable
● The object in the update() method does not have to be a set, it can
be any iterable object (tuples, lists, dictionaries etc.).
Python - Remove Set Items
● Remove Item
● To remove an item in a set, use the remove(), or the discard()
method.
Example
Python - Remove Set Items
● You can also use the pop() method to remove an item, but this
method will remove a random item, so you cannot be sure what
item that gets removed.
● The return value of the pop() method is the removed item.
Python - Remove Set Items
● You can also use the pop() method to remove an item, but this
method will remove a random item, so you cannot be sure what
item that gets removed.
● The return value of the pop() method is the removed item.
Example
Loop Items
● You can loop through the set items by using a for loop:
Python - Join Sets
● Join Two Sets
● There are several ways to join two or more sets in Python.
● You can use the union() method that returns a new set containing
all items from both sets, or the update() method that inserts all the
items from one set into another:
Python - Join Sets
Keep ONLY the Duplicates
● The intersection_update() method will keep only the items that are
present in both sets.
Keep ONLY the Duplicates
● The intersection() method will return a new set, that only contains
the items that are present in both sets.
Keep All, But NOT the Duplicates
● The symmetric_difference_update() method will keep only the
elements that are NOT present in both sets.
Keep All, But NOT the Duplicates
● The symmetric_difference() method will return a new set, that
contains only the elements that are NOT present in both sets.
Keep All, But NOT the Duplicates
● The symmetric_difference() method will return a new set, that
contains only the elements that are NOT present in both sets.
NOTES
● Sets are unordered, so you cannot be sure in which order the items
will appear.
● Once a set is created, you cannot change its items, but you can
remove items and add new items.
● The values True and 1 are considered the same value in sets, and
are treated as duplicates:
● If the item to remove does not exist, remove() will raise an error.
● If the item to remove does not exist, discard() will NOT raise an
error.
● Both union() and update() will exclude any duplicate items.
Study
● Method
○ isdisjoint()
○ issubset()
○ issuperset()
Thank
You