Python Sets

In Python, a set is an unordered collection of unique elements. Sets are useful when you need to store and manage data without duplicates. This guide covers how to create and work with sets, along with common set methods.

Creating a Set

You can create a set using curly braces {} or the set() function.

# Creating sets
empty_set = set()  # An empty set
fruits = {"apple", "banana", "cherry"}  # A set with unique elements

print(fruits)  # Output: {'banana', 'cherry', 'apple'}

Try It Now

Note: Sets do not maintain the order of elements, and duplicate elements are automatically removed.

# Duplicates are removed
numbers = {1, 2, 2, 3, 4, 4, 5}
print(numbers)  # Output: {1, 2, 3, 4, 5}

Try It Now

Common Set Methods

Python sets come with several built-in methods for performing common set operations.

1. add()

Adds an element to the set.

# Example of add()
fruits = {"apple", "banana"}
fruits.add("cherry")

print(fruits)  # Output: {'apple', 'banana', 'cherry'}

Try It Now

2. remove()

Removes a specified element from the set. Raises a KeyError if the element is not found.

# Example of remove()
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")

print(fruits)  # Output: {'apple', 'cherry'}

Try It Now

3. discard()

Removes a specified element from the set. Unlike remove(), it does not raise an error if the element is not found.

# Example of discard()
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")

print(fruits)  # Output: {'apple', 'cherry'}

Try It Now

4. pop()

Removes and returns an arbitrary element from the set.

# Example of pop()
fruits = {"apple", "banana", "cherry"}
removed_item = fruits.pop()

print(removed_item)  # Output: (random element)
print(fruits)  # Remaining elements

Try It Now

5. clear()

Removes all elements from the set.

# Example of clear()
fruits = {"apple", "banana", "cherry"}
fruits.clear()

print(fruits)  # Output: set()

Try It Now

Set Operations

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

1. union()

Returns a new set containing all elements from both sets.

# Example of union()
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)

print(result)  # Output: {1, 2, 3, 4, 5}

Try It Now

2. intersection()

Returns a new set containing only the common elements.

# Example of intersection()
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.intersection(set2)

print(result)  # Output: {3}

Try It Now

3. difference()

Returns a new set containing elements that are in the first set but not in the second.

# Example of difference()
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.difference(set2)

print(result)  # Output: {1, 2}

Try It Now

4. symmetric_difference()

Returns a new set containing elements that are in either set, but not both.

# Example of symmetric_difference()
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.symmetric_difference(set2)

print(result)  # Output: {1, 2, 4, 5}

Try It Now

Conclusion

Python sets are a powerful tool for managing collections of unique elements. Understanding how to use sets and their methods will help you perform operations like union, intersection, and difference efficiently.