Set in Python

Characteristics:

  1. Unordered: Sets do not maintain any particular order of elements. The order in which elements are added does not necessarily match the order in which they are stored.
  2. Mutable: Sets can be modified after their creation. You can add or remove elements from a set.
  3. Unique Elements: Sets automatically remove duplicate elements. Each element in a set must be unique.
  4. Unindexed: Sets do not support indexing. You cannot access set elements by their position.
  5. Heterogeneous: Sets can contain elements of different data types, including other sets (but not mutable types like lists or dictionaries).

Syntax:

Sets are defined by placing a comma-separated sequence of values inside curly braces {} or by using the set() function.

my_set = {1, 2, 3}
another_set = set(['a', 'b', 'c'])
Creating Sets

You can create sets using curly braces {} or the set() constructor.

  1. Using Curly Braces:

    my_set = {1, 2, 3, 4}
    print(my_set)  # Output: {1, 2, 3, 4}
    
  2. Using the set() Constructor:

    another_set = set([1, 2, 3, 4])
    print(another_set)  # Output: {1, 2, 3, 4}
    
Adding Elements to a Set

You can add elements to a set using the add() and update() methods.

  1. add(): Adds a single element to the set.

    my_set = {1, 2, 3}
    my_set.add(4)
    print(my_set)  # Output: {1, 2, 3, 4}
    
  2. update(): Adds multiple elements to the set. Accepts any iterable.

    my_set = {1, 2, 3}
    my_set.update([4, 5])
    print(my_set)  # Output: {1, 2, 3, 4, 5}
    
Removing Elements from a Set

You can remove elements using the remove(), discard(), and pop() methods.

  1. remove(): Removes a specified element. Raises a KeyError if the element is not found.

    my_set = {1, 2, 3, 4}
    my_set.remove(3)
    print(my_set)  # Output: {1, 2, 4}
    
  2. discard(): Removes a specified element. Does not raise an error if the element is not found.

    my_set = {1, 2, 3, 4}
    my_set.discard(3)
    print(my_set)  # Output: {1, 2, 4}
  3. pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.

    my_set = {1, 2, 3, 4}
    element = my_set.pop()
    print(element)  # Output: (One of the elements from the set)
    print(my_set)  # Output: (Remaining elements in the set)
    
  4. clear(): Removes all elements from the set.

    my_set = {1, 2, 3, 4}
    my_set.clear()
    print(my_set)  # Output: set()
    

Set Operations

Sets support various mathematical operations like union, intersection, difference, and symmetric difference.

  1. union(): Returns a set containing all elements from both sets.

    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    union_set = set1.union(set2) # or a | b 
    print(union_set)  # Output: {1, 2, 3, 4, 5}
    
  2. intersection(): Returns a set containing only the common elements between sets.

    set1 = {1, 2, 3}
    set2 = {2, 3, 4}
    intersection_set = set1.intersection(set2) # (or) a & b
    print(intersection_set)  # Output: {2, 3}
    
  3. difference(): Returns a set containing elements that are in the first set but not in the second set.

    set1 = {1, 2, 3}
    set2 = {2, 3, 4}
    difference_set = set1.difference(set2) # or a-b
    print(difference_set)  # Output: {1}
    
  4. symmetric_difference(): Returns a set containing elements that are in either set, but not in both.

    set1 = {1, 2, 3}
    set2 = {2, 3, 4}
    sym_diff_set = set1.symmetric_difference(set2) # (or) a ^ b
    print(sym_diff_set)  # Output: {1, 4}
    
Code Example