Skip to content

Latest commit

 

History

History
174 lines (123 loc) · 6.5 KB

sets.md

File metadata and controls

174 lines (123 loc) · 6.5 KB

Sets

Definition and Initialization

A set in Python is an unordered collection of unique elements. Sets are written with curly braces {} and can contain elements of different data types. They are particularly useful for membership testing, removing duplicates from a sequence, and mathematical operations like union, intersection, difference, and symmetric difference.

mySet = {1, 2, 3}  # A set of integers
emptySet = set()  # An empty set (cannot use {})

Accessing Elements

Unlike lists or dictionaries, sets do not support indexing, slicing, or other sequence-like behavior. However, you can check for the presence of an element using the in keyword.

if 1 in mySet:
    print("1 is in the set")

Adding and Removing Elements

Elements can be added to a set using the add() method, and multiple elements can be added using the update() method. To remove elements, you can use the remove() or discard() methods.

mySet.add(4)  # Adds an element to the set
mySet.update([5, 6])  # Adds multiple elements to the set

mySet.remove(6)  # Removes an element; raises KeyError if not found
mySet.discard(5)  # Removes an element; does nothing if not found

Set Methods

Python provides various methods for sets:

mySet.add(element)  # Adds an element to the set
mySet.remove(element)  # Removes the specified element. Raises a KeyError if the element is not present.
mySet.pop()  # Removes and returns an arbitrary set element. Raises KeyError if empty.
mySet.update(anotherSet)  # Update the set with the union of this set and others
mySet.union(anotherSet)  # Return a set containing the union of sets
mySet.clear()  # Removes all elements from the set
mySet.copy()  # Returns a shallow copy of the set
mySet.difference(anotherSet)  # Returns a set containing the difference between two or more sets
mySet.difference_update(anotherSet)  # Removes the items in this set that are also included in another specified set
mySet.discard(element)  # Remove the specified item
mySet.intersection(anotherSet)  # Returns a set, that is the intersection of two or more sets
mySet.intersection_update(anotherSet)  # Removes the items in this set that are not present in other specified sets
mySet.isdisjoint(anotherSet)  # Returns whether two sets have a intersection or not
mySet.issubset(anotherSet)  # Returns whether another set contains this set or not
mySet.issuperset(anotherSet)  # Returns whether this set contains another set or not
mySet.symmetric_difference(anotherSet)  # Returns a set with the symmetric differences of two sets
mySet.symmetric_difference_update(anotherSet)  # inserts the symmetric differences from this set and another

Set Operations

Sets support mathematical operations like union (|), intersection (&), difference (-), and symmetric difference (^).

a = {1, 2, 3}
b = {3, 4, 5}

union = a | b  # {1, 2, 3, 4, 5}
intersection = a & b  # {3}
difference = a - b  # {1, 2}
symmetric_difference = a ^ b  # {1, 2, 4, 5}

Iterating Over a Set

You can iterate over the elements of a set using a for loop. Remember that sets are unordered, so the order of elements might not be the same every time.

for item in mySet:
    print(item)

Frozen Sets

Frozen sets are like sets, but they are immutable. You cannot add or remove elements from a frozen set.

frozen = frozenset([1, 2, 3])

Example of Using a Set

Here's an example demonstrating the declaration, initialization, and use of a set in Python:

colors = {'red', 'green', 'blue'}

# Adding an element
colors.add('yellow')

# Iterating over a set
for color in colors:
    print(color)

# Checking membership
if 'red' in colors:
    print("Red is present")

Caveats and Considerations

  • Sets do not record element position or order of insertion, which means sets do not support indexing, slicing, or other sequence-like behavior.
  • The elements in a set must be immutable, like numbers, strings, or tuples.
  • Because sets are implemented as hash tables, the elements of a set must be hashable.
  • Set operations like adding, removing, or checking membership are generally O(1), making them efficient for large datasets.

Comparing Sets

Sets can be compared using operators like ==, !=, <= (is subset), and >= (is superset).

a = {1, 2, 3}
b = {1, 2}
c = {1, 2, 3}

a == c  # True
b <= a  # True (b is a subset of a)
a >= b  # True (a is a superset of b)
a != c  # False

Converting Between Sets and Other Data Types

You can convert sets to lists, tuples, and vice versa.

myList = [1, 2, 3, 1, 2]
mySet = set(myList)  # {1, 2, 3}
myTuple = tuple(mySet)  # (1, 2, 3)

Making Custom Objects Hashable

Explain how to make a custom object hashable so it can be used in a set. This involves implementing the hash() and eq() methods in a class.

class MyObject:
    def __init__(self, value):
        self.value = value

    def __hash__(self):
        return hash(self.value)

    def __eq__(self, other):
        if isinstance(other, MyObject):
            return self.value == other.value
        return False

obj1 = MyObject(1)
obj2 = MyObject(1)
mySet = {obj1, obj2}  # Only one object is added because they are considered equal

hash

  • Purpose: The hash method in Python returns the hash value of an object, which is an integer used to quickly compare dictionary keys during a dictionary lookup. Hash values are also used in sets to determine if an object is already present.

  • Behavior: When you implement a custom class, Python provides a default hash method. However, if you override eq, Python automatically sets hash to None unless you explicitly implement it. This is because the default hash implementation is not suitable for objects that are considered equal in a custom way through eq.

  • Return Value: It should return an integer. The rule of thumb is that objects that are equal (i.e., their eq method returns True when comparing them) must have the same hash value.

eq

  • Purpose: The eq method is used to define a custom equality comparison between objects. It's called when you use the == operator.

  • Behavior: By default, eq compares the memory addresses of objects (i.e., it checks if they are the same object). By overriding eq, you can define custom logic for how two objects of the same class should be compared based on their content or attributes.

  • Return Value: It should return a Boolean (True or False), indicating whether the objects are considered equal.