Programming in Python: Sets

Introduction to Sets

A set is a built-in data type in Python that represents an unordered collection of unique elements. Sets are useful when dealing with collections of items where duplicates are not allowed, and set operations (like union, intersection, and difference) can be performed efficiently.

Creating a Set

We can create a set from a list or any iterable using the set() function:

list1 = ['apple', 'mango', 'pineapple', 'mango', 'apple', 'orange']
fruit = set(list1)
print(fruit)

Output:

{'orange', 'mango', 'apple', 'pineapple'}

As seen above, duplicate values ('mango' and 'apple') are automatically removed.

Checking Membership

You can check if an element is present in a set using the in keyword:

print('mango' in fruit)  # True
print('grape' in fruit)  # False

Set Operations

Python sets support various operations such as union, intersection, difference, and symmetric difference.

Example: Set Operations on Strings

We can also create sets from strings, which treat each character as a unique element.

a = 'abhilash'
b = 'abhijith'

set_a = set(a)
set_b = set(b)

print("Set A:", set_a)
print("Set B:", set_b)

Output:

Set A: {'a', 'b', 'i', 'h', 'l', 's'}
Set B: {'a', 'b', 'i', 'h', 'j', 't'}

Difference (-)

Elements in set_a but not in set_b:

print(set_a - set_b)  # {'s', 'l'}

Elements in set_b but not in set_a:

print(set_b - set_a)  # {'j', 't'}

Union (|)

Combines all elements from both sets:

print(set_a | set_b)  # {'a', 'b', 'i', 'h', 'j', 'l', 's', 't'}

Intersection (&)

Common elements in both sets:

print(set_a & set_b)  # {'a', 'b', 'i', 'h'}

Symmetric Difference (^)

Elements unique to each set (not in both):

print(set_a ^ set_b)  # {'s', 'l', 'j', 't'}

Additional Set Methods

Python provides several built-in methods for working with sets:

# Adding elements
a_set = {1, 2, 3}
a_set.add(4)
print(a_set)  # {1, 2, 3, 4}

# Removing elements
a_set.remove(2)
print(a_set)  # {1, 3, 4}

# Discarding elements (won't raise an error if element is absent)
a_set.discard(5)

# Clearing a set
a_set.clear()
print(a_set)  # set()

When to Use Sets

  • Removing duplicate values from a list.
  • Checking for membership (in is faster in sets than in lists).
  • Performing mathematical set operations like union, intersection, and difference.

Conclusion

Sets in Python provide an efficient way to handle collections of unique elements and perform operations like union, intersection, and difference. By understanding these fundamental operations, you can use sets to write cleaner and more efficient Python code.