BACSE101
Problem Solving Using Python
Sets
Module 3 Data Comprehension –
Iterators - Data Selection -
Sets Ordered, unordered and
Unique Data Organization -
Data Modification - Grouping
and Categorization
What is a Set in Python?
• a set is an unordered collection of unique and
immutable elements.
• It's a direct implementation of the
mathematical concept of a set.
Why Use Sets?
• Efficient membership testing.
• Eliminating duplicate entries.
• Performing mathematical set operations
(example: union, intersection ).
Key Properties:
• Unordered: Elements have no defined order.
• You cannot access elements by an index.
• (like my_set[0] won't work)
• Unique Elements: Each element in a set must be distinct.
• If you add a duplicate, it's automatically ignored.
• Mutable (The Set Itself): You can add new elements to a set or
remove existing ones after it's created.
• Elements Must Be Immutable: The items stored inside a set
must be hashable (immutable).
• This includes numbers, strings, tuples, and frozenset objects.
• Mutable types like lists or dictionaries cannot be direct
elements of a set.
Creating Sets in Python
Two primary ways to create a set in Python:
• Using Curly Braces {}: For non-empty sets.
• Using the set() Constructor: For empty sets
or converting other iterables (like lists,
tuples, strings) into sets.
Note:
• my_empty_set = {} creates an empty dictionary, not an empty set.
• Always use set() for an empty set
Examples
Adding and Removing Elements
• Sets are mutable, meaning you can change their contents after creation.
• add(element): Adds a single element to the set. If the element already
exists, nothing happens.
• update(iterable): Adds multiple elements from an iterable (like a list or
another set).
• remove(element): Removes a specified element. Raises a KeyError if the
element is not found.
• discard(element): Removes a specified element. Does not raise an error if
the element is not found.
• pop(): Removes and returns an arbitrary (random) element from the set.
Raises a KeyError if the set is empty.
• clear(): Removes all elements from the set.
Examples
Set Operations: Subsets and Supersets
• Subset (<= or .issubset()): A set A is a subset of set B if all
elements of A are also elements of B.
• Proper Subset (<): A set A is a proper subset of set B if A is
a subset of B and A is not equal to B.
• Superset (>= or .issuperset()): A set A is a superset of set B
if all elements of B are also elements of A.
• Proper Superset (>): A set A is a proper superset of set B if
A is a superset of B and A is not equal to B.
Examples
frozenset (Immutable Sets)
• A frozenset is an immutable version of a Python set.
• Once created, its elements cannot be added, removed, or
changed.
Why use frozenset?
• Since frozenset objects are immutable and hashable, they can be
used as elements within other set, or as keys in dictionary objects.
• Regular (mutable) sets cannot be used in these contexts.
Creation: Use the frozenset() constructor.
Examples
Set Comprehension - Iterators
• A concise and efficient way to create sets from
other iterables.
• Similar to list comprehensions, but uses curly
braces {}.
Syntax: {expression for item in iterable if condition}
Example: Iterating over a Set
Set Data Selection – Membership and Retrieval
• Checking for Membership (in operator):
• Sets offer highly optimized constant-time
average complexity for checking if an
element exists.
Sets: Ordered, Unordered, and Unique Data
Organization
• Unordered Nature:
• Elements in a set do not have a specific index or position.
• You cannot access elements using my_set{0}.
• This is a fundamental difference from lists and tuples.
• Unique Data Storage:
• Sets automatically enforce uniqueness.
• If you try to add an existing element, it's ignored.
Example
Set Data Modification
Adding Elements:
• add(element): Adds a single element to the set. If the
element already exists, the set remains unchanged.
• update(iterable): Adds all elements from an iterable (list, tuple,
another set, etc.) to the set
Set Operations for Grouping and Categorization
• Union (| or union()): Combines all unique elements
from both sets.
• Used to find all distinct elements from two or more
groups.
• Intersection (& or intersection()): Returns elements
common to both sets.
• Used to find common members between categories.
• Difference (- or difference()): Returns elements present in
the first set but not in the second.
• Used to find elements unique to one category when
compared to another.
• Symmetric Difference (^ or symmetric_difference()):
Returns elements that are in either set, but not in both.
• Used to find elements that are unique across two
categories.
Sets: Real-world Use Cases
•Data Cleaning:
• Quickly remove duplicate entries from a list of items (e.g., email
addresses, product IDs). unique_emails = set(all_emails_list)
•Recommendation Systems:
• Find common interests between users (intersection of liked items).
•Text Analysis:
• Identify unique words in a document.
• Compare vocabulary between two texts.
•Network Analysis:
• Find common connections or unique connections between nodes.
• Database Operations (Conceptual):
• Mimic SQL-like UNION, INTERSECT, EXCEPT operations on data.