In Python, a set is an unordered collection of unique elements. It's a powerful data structure that offers efficient membership testing and eliminates duplicate values automatically. Sets are mutable, allowing you to add or remove items, but the elements themselves must be immutable.
You can create a set using curly braces {}
or the set()
constructor. Here are two examples:
# Using curly braces
fruits = {"apple", "banana", "cherry"}
# Using the set() constructor
colors = set(["red", "green", "blue"])
Note that an empty set must be created using set()
, as {}
creates an empty dictionary.
Python sets support various operations that make them useful for mathematical set operations and data manipulation:
Here's an example demonstrating these operations:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
union_set = set1 | set2 # {1, 2, 3, 4, 5, 6, 7, 8}
intersection_set = set1 & set2 # {4, 5}
difference_set = set1 - set2 # {1, 2, 3}
symmetric_difference_set = set1 ^ set2 # {1, 2, 3, 6, 7, 8}
Python sets provide several useful methods for manipulation:
add()
: Add an element to the setremove()
: Remove a specific element (raises KeyError if not found)discard()
: Remove an element if it exists (no error if not found)pop()
: Remove and return an arbitrary elementclear()
: Remove all elements from the setSets are particularly useful in scenarios where you need to:
For example, to remove duplicates from a list:
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 5]
unique_numbers = list(set(numbers)) # [1, 2, 3, 4, 5]
Similar to Python List Comprehensions, you can use set comprehensions to create sets concisely:
squares = {x**2 for x in range(10)} # {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}
Sets offer O(1) average time complexity for add, remove, and membership testing operations, making them highly efficient for large datasets. However, they consume more memory than lists due to their hash table implementation.
Python sets are versatile data structures that excel in scenarios requiring unique elements and set operations. By leveraging their efficiency and built-in methods, you can simplify your code and improve performance in various programming tasks.
To further enhance your Python skills, explore related concepts such as Python Dictionaries and Python Set Operations.