5/7/2025
Python Programming - Sets
Internal use only
The sets
• A set is an unordered collection (meaning that the order of elements is not preserved) with no
duplicate items
• Sets are primarily used for membership testing and eliminating duplicate entries
• The curly braces { } or the set() function can be used to create sets with a comma-separated list of
items inside curly brackets { }
• To create an empty set, you have to use set() and not { } as the latter creates an empty dictionary
1
5/7/2025
The sets
• Sets support mathematical operations, such as union, intersection, difference and symmetric
difference
Set methods
Method Syntax Description
add() set_name.add(item) Adds a single item to the set set_name.
clear() set_name.clear() Removes all the items from the set set_name.
Returns a new set with items in the set set_name and that are not in the
difference() set_name.difference(*others)
others sets.
discard() set_name.discard(item) Removes an item from the set set_name if it is present.
Returns a new set with items common to the set set_name and all others
intersection() set_name. intersection(*others)
sets.
Returns True if the set set_name has no items in common with the other
isdisjoint() set_name.isdisjoint(other)
set. Sets are disjoint if and only if their intersection is an empty set.
issubset() set_name.issubset(other) Returns True if every item in the set set_name is in the other set.
issuperset() set_name.issuperset() Returns True if every item in the other set is in the set_name.
Removes and returns an arbitrary item from the set set_name. It raises
pop() set_name.pop()
KeyError if the set set_name is empty.
Removes and returns an item from the set set_name. It raises KeyError if
remove() set_name.remove(item)
the item is not contained in the set set_name.
symmetric_diff set_name.symmetric_difference( Returns a new set with items either in the set_name or in other but not
erence() other) both.
union() set_name.union(*others) Returns a new set with items from the set set_name and all others sets.
update() set_name.update(*others) Updates the set set_name by adding items from all others sets.
4
2
5/7/2025
Traversing of sets
• The for loop can be used to iterate through each item in a set
Frozenset
• A frozenset is basically the same as a set, except that it is immutable
• Once a frozenset is created, then its items cannot be changed
• Since the frozenset is immutable, they can be used as members in other sets and as dictionary
keys
• The frozensets have the same functions as normal sets, except none of the functions that change
the contents (update, remove, pop, etc.) are available
3
5/7/2025
Sets vs lists - key differences
Order and uniqueness
• Sets: unordered and unique elements - no duplicates allowed
• Lists: ordered and can contain duplicate elements
Mutability
• Sets: mutable, but only immutable objects can be stored in it (you can add or remove elements, but
you cannot change the elements themselves)
• Lists: mutable, and elements can be changed, added, or removed
Access and performance
• Sets: does not support indexing or slicing - elements cannot be accessed using subscript notation -
sets are optimized for membership tests using the in operator, making them faster for checking if an
element exists
• Lists: supports indexing and slicing - elements can be accessed using subscript notation - lists
allow random access to elements
Sets vs lists - key differences
Syntax and usage
• Sets: created using curly braces {} or the set() constructor - cannot create an empty set using {};
use set() instead
• Lists: created using square brackets [] or the list() constructor - allows nested lists
4
5/7/2025
Examples
1) Choosing between a set and a list depends on the specific
requirements of your use case
2) Use a set when you need to store unique elements and
perform fast membership tests
3) Use a list when you need to maintain the order of elements
and allow duplicates