Python Sets: A Comprehensive Guide with Python Set Examples

Python Basics

A set is one of the fundamental data structures in computer science, used to store a collection of distinct elements without any particular order. In this article, we will explore sets, their properties, and how they can be implemented using Python.

Table of Contents

Introduction to Sets

In Python, sets are built-in data types that can hold any immutable (non-changeable) objects such as strings, numbers, or tuples. They do not allow duplicate elements, unlike lists. To create a set in Python, you can use curly braces {} or the built-in function set().


# Using curly braces
custom_set = {1, 2, 'a', 'g'}
print(custom_set) # Output: {'a', 1, 2, 'g'}

# Using set() function
numbers = set([1, 3, 10, 4])
print(numbers) # Output: {1, 10, 3, 4}

Python Set Operations

Sets support various operations that can be performed on them. These operations include union, intersection, difference, and symmetric difference. Let’s look at some examples using the numbers and letters sets defined above:

Python Set Operation – Union

The union of two sets returns a new set containing all unique elements from both sets. In Python, you can use the | operator to perform this operation.


union = numbers | custom_set
print(union) # Output: {1, 2, 3, 4, 'g', 'a', 10}

Python Set Operation – Intersection

The intersection of two sets returns a new set containing all elements that are common to both sets. In Python, you can use the & operator for this operation.


intersection = numbers & custom_set
print(intersection) # Output: {1}

Python Set Operation – Difference

The difference between two sets returns a new set containing all elements from the first set that are not present in the second set. In Python, you can use the - operator for this operation.


difference = numbers - custom_set
print(difference) # Output: {10, 3, 4} 

Symmetric Difference

The symmetric difference of two sets returns a new set containing all unique elements from both sets that are not common between them. In Python, you can use the ^ operator for this operation.


sym_diff = numbers ^ custom_set
print(sym_diff) # Output: {2, 3, 4, 10, 'a', 'g'} (Elements exclusive to either set or common ones removed)

Python Set Membership and Operations

You can check if an element is present in a set using the in keyword. Additionally, Python provides methods for performing set operations directly on sets without creating new variables. Let’s look at some examples:


if 10 in numbers:
    print("Element '10' exists in numbers set")
else:
    print("Element '10' does not exist in numbers set")

# Using set operations directly on sets
numbers_intersection = numbers & custom_set
print(f"Intersection of numbers and custom_set is {numbers_intersection}")

Conclusion

Sets are a powerful data structure that can be used to store distinct elements without any particular order. Python provides built-in support for sets, allowing you to perform various operations on them easily. Understanding the properties and usage of sets will help you write more efficient and readable code in your projects.

To top