set¶
- sets represent unordered collections of unique elements
- Eliminate duplicate entries: Sets automatically remove duplicates, ensuring each element appears only once.
- Check for membership: Quickly determine if an element exists in the set using the
in
operator. - Perform set operations: Sets support mathematical operations like union (combining elements), intersection (finding common elements), and difference (elements in one set but not the other).
- Set can be created using curly braces
{}
or usingset()
function - Key Characteristics
- Unordered: Elements in a set don't have a specific order, and iterating over a set won't guarantee the order in which you get the elements.
- Unchangeable: Sets themselves are immutable, meaning you cannot modify the elements after creation. However, you can create a new set with changes.
- Accessing Elements
- Since sets are unordered, you cannot access elements by index. However, you can check for membership using the
in
operator
- Since sets are unordered, you cannot access elements by index. However, you can check for membership using the
- Adding or Removing Elements
- Sets are mutable in the sense that you can add or remove elements, but you cannot modify existing elements
add(element)
: Add a new, unique element to the set.remove(element)
: Remove the first occurrence of an element (error if not found).discard(element)
: Similar to remove, but ignores the error if the element is not found.pop()
: Removes and returns an arbitrary element from the set (raises a KeyError if the set is empty).update(iterable)
: Update the set by adding elements from another iterable (like a list or another set).
- Sets are mutable in the sense that you can add or remove elements, but you cannot modify existing elements
- Set Operations
union |
: Combines elements from both sets, excluding duplicates.intersection &
: Returns elements that are common to both sets.difference -
: Returns elements in the first set that are not in the second set.symmetric_difference ^
: Returns elements that are in either set but not in both.
- Common Set Methods
len(set)
: Returns the number of elements in the set.isdisjoint(other_set)
: Checks if two sets have no common elements (are disjoint).issubset(other_set)
: Checks if all elements in the first set are also in the other set.issuperset(other_set)
: Checks if the first set contains all elements of the other set and may have additional elements.
- When to Use Sets
- Use sets when you need to store unique elements and perform membership checks efficiently.
- They are useful for deduplicating data, performing set operations, and working with collections where order doesn't matter.
- Key Points
- Sets are unordered collections with unique elements.
- They are useful for eliminating duplicates, checking membership, and performing set operations.
- Sets are mutable in the sense that you can add or remove elements, but the elements themselves are immutable.
- Understand the different set methods and operators to effectively work with sets in your Python programs
In [ ]:
Copied!
fruits = {"apple", "banana", "cherry", "apple"} # Duplicates will be removed
numbers_list = [1, 2, 2, 3, 4]
numbers_set = set(numbers_list) # Converts a list with duplicates to a set (duplicates removed)
if "mango" in fruits:
print("Mango is in the set.")
fruits = {"apple", "banana", "cherry", "apple"} # Duplicates will be removed
numbers_list = [1, 2, 2, 3, 4]
numbers_set = set(numbers_list) # Converts a list with duplicates to a set (duplicates removed)
if "mango" in fruits:
print("Mango is in the set.")