Sets in Python

What is a Set? A set is a collection of unordered, unique elements. Unlike lists and tuples, sets do not allow duplicate values. Sets are mutable, meaning you can add or remove elements, but they do not maintain any order of elements.

How to Create a Set

You can create a set in Python by using curly braces {}. The set will automatically remove any duplicate elements.

Example: Creating a Set

Here’s an example of how to create a set:

# Example: Creating a Set
my_set = {1, 2, 3, 4, 5}
print(my_set)
    

Explanation: This creates a set called my_set with five unique elements. The set is unordered, so the elements may not appear in the same order as they were added.

Sets do not allow Duplicate Elements

If you try to add duplicate elements to a set, Python will automatically remove them.

Example: Duplicate Elements in a Set

Let’s see how Python handles duplicate values:

# Example: Duplicate Elements in a Set
my_set = {1, 2, 2, 3, 4, 4, 5}
print(my_set)  # Duplicate values (2 and 4) are removed
    

Explanation: Although we tried to add the values 2 and 4 twice, Python automatically removes the duplicates and keeps only the unique elements.

Accessing Elements in a Set

Since sets are unordered, you cannot access set elements using an index like in lists or tuples. However, you can loop through the set to access each element.

Example: Looping Through a Set

Here’s how to loop through a set to access its elements:

# Example: Looping Through a Set
my_set = {1, 2, 3, 4, 5}
for element in my_set:
    print(element)
    

Explanation: We use a for loop to iterate over each element in the set and print it. The order may vary each time you run the code because sets are unordered.

Adding and Removing Elements from a Set

Sets are mutable, which means you can add or remove elements after the set has been created.

Example: Adding Elements to a Set

We can add elements to a set using the add() method:

# Example: Adding Elements to a Set
my_set = {1, 2, 3}
my_set.add(4)  # Adding an element to the set
print(my_set)  # Set now contains {1, 2, 3, 4}
    

Explanation: We use the add() method to add the element 4 to the set. If the element is already in the set, it will not be added again because sets do not allow duplicates.

Example: Removing Elements from a Set

We can remove elements from a set using the remove() or discard() method:

# Example: Removing Elements from a Set
my_set = {1, 2, 3, 4}
my_set.remove(3)  # Removes the element 3
print(my_set)  # Set now contains {1, 2, 4}
    

Explanation: The remove() method removes the element 3 from the set. If the element is not present, it raises an error. You can also use discard(), which does not raise an error if the element is not found.

Set Operations

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

Example: Set Union

The union of two sets combines all the elements from both sets, removing duplicates:

# Example: Set Union
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)  # Output: {1, 2, 3, 4, 5}
    

Explanation: The union() method combines the elements from set1 and set2, keeping only unique elements.

Example: Set Intersection

The intersection of two sets gives only the common elements between them:

# Example: Set Intersection
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {3}
    

Explanation: The intersection() method returns only the elements that are present in both sets.

Example: Set Difference

The difference of two sets gives elements that are in the first set but not in the second:

# Example: Set Difference
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1, 2}
    

Explanation: The difference() method returns the elements that are in set1 but not in set2.

Example: Set Symmetric Difference

The symmetric difference of two sets gives elements that are in either set, but not in both:

# Example: Set Symmetric Difference
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set)  # Output: {1, 2, 4, 5}
    

Explanation: The symmetric_difference() method returns elements that are in either set1 or set2, but not in both.

Summary

Definition: A set in Python is a collection of unordered and unique elements. Unlike lists or tuples, sets do not allow duplicates and do not maintain any specific order.

Creating a Set: You can create a set by using curly braces {}. If duplicate elements are added, Python will automatically remove them.

Example:

# Creating a Set
my_set = {1, 2, 3, 4, 5}
      

Duplicate Elements: Sets automatically remove duplicates, so only unique values are kept in a set.

Example:

# Removing Duplicate Elements
my_set = {1, 2, 2, 3, 4, 4, 5}
print(my_set)  # Output: {1, 2, 3, 4, 5}
      

Key Operations: Sets support operations like union, intersection, difference, and symmetric difference to perform mathematical set operations.