No posts available in this category.

Python Sets

Add to Favorites

Sets in Python are a data structure that provide a powerful way to store and manipulate collections of unique items.

Sets are most often used when dealing with mathematical operations, filtering data, or ensuring that a collection contains no duplicates.

Sets are also mutable, meaning elements can be added or removed after a set is created.

Basic set syntax:

python
set_example = {item1, item2, item3} fruits = {"apple", "banana", "grape"}

Creating Sets

There are several ways to create sets in Python.

Creating a set using curly braces {}:

python
my_set = {1, 2, 3}

Creating a set using the set() constructor:

python
my_set = set([1, 2, 3])

Creating an empty set:

python
empty_set = {} empty_set = set()

Creating a set from a string:

python
string_set = set("hello") print(string_set) # Output {'l', 'e', 'o', 'h'}

In the above example, the order of the set created from a string may vary due to sets unorderd property

Basic Set Operations

Python sets support various operations that allow the data within the set to be manipulated and analyzed.

Adding Elements To A Set

To add a single element to a set, use the add() method:

python
fruits = {"apple", "banana"} fruits.add("cherry") print(fruits) # Output: {'apple', 'banana', 'cherry'}

To add multiple elements to a set, use the update() method:

python
fruits = {"apple", "banana"} fruits.update(["orange", "grape"]) print(fruits) # Output: {'apple', 'banana', 'cherry', 'orange', 'grape'}

Removing Elements From A Set

To remove the specified element from a set, use the remove() method:

python
fruits = {"apple", "banana"} fruits.remove("banana") print(fruits) # Output: {'apple', 'cherry'}

Keep in mind, the remove() method raises a KeyError if the specified element is not found.

To remove the specified element from a set without raising a KeyError if the element is not found, use the discard() method:

python
fruits = {"apple", "banana"} fruits.discard("banana")

To remove and return an unspecified element from the set, use the pop() method:

python
fruits = {"apple", "banana"} fruit = fruits.pop() print(fruit) # Output: 'apple' (or any other element in the set)

To remove all elements from a set, use the clear() method:

python
fruits = {"apple", "banana"} fruits.clear() print(fruits) # Output: {}

Checking For Membership

To check if an element is in a set, use the in keyword:

python
fruits = {"apple", "banana", "cherry"} print("banana" in fruits) # Output: True print("grape" in fruits) # Output: False

Set Operations

Python sets provide built-in methods for performing mathematical set operations like union, intersection, difference, and symmetric difference.

Union (| or union())

To create a new set that contains all elements of both sets, use | or the union() method:

python
set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1 | set2 # or union_set = set1.union(set2) print(union_set) # Output: {1, 2, 3, 4, 5}

Intersection (& or intersection())

To return a new set that contains only elements that appear in both sets, use & or the intersection() method:

python
set1 = {1, 2, 3} set2 = {3, 4, 5} intersection_set = set1 & set2 # or intersection_set = set1.intersection(set2) print(intersection_set) # Output: {3}

Difference (- or difference())

To return a new set that contains elements that are in the first set but not the second, use - or the difference() method:

python
set1 = {1, 2, 3} set2 = {3, 4, 5} difference_set = set1 - set2 # or difference_set = set1.difference(set2) print(difference_set) # Output: {1, 2}

Symmetric Difference (^ or symmetric_difference())

To return a new set that contains elements that are in either of the sets but not both, use ^ or the symmetric_difference() method:

python
set1 = {1, 2, 3} set2 = {3, 4, 5} symmetric_difference_set = set1 ^ set2 # or symmetric_difference_set = set1.symmetric_difference(set2) print(symmetric_difference_set) # Output: {1, 2, 4, 5}

Set Methods

Python sets have several built-in methods that allow for more advanced operations like copying, and comparing sets.

To create a shallow copy of a set, use the copy() method:

python
set1 = {1, 2, 3} set_copy = set1.copy

To check if all elements of one set are present in another, use the issubset() method:

python
set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5} print(set1.issubset(set2)) # Output: True

To check if one set contains all elements of another, use the issuperset() method:

python
set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5} print(set2.issuperset(set1)) # Output: True

To check if two sets have no elements in common, use the isdisjoint() method:

python
set1 = {1, 2, 3} set2 = {4, 5, 6} print(set1.isdisjoint(set2)) # Output: True

Set Performance

Sets are implemented using hash tables, which make there average time complexity for membership tests (in), insertion, and deletion operations O(1). Sets are very efficient for tasks involving unique items and fast lookups.

Practical Applications Of Sets

To remove duplicates from a list, use the set()function:

python
names = ["Alice", "Bob", "Alice", "Eve"] unique_names = list(set(names)) print(unique_names) # Output: ['Alice', 'Bob', 'Eve'] (order may vary)

To find common elements between multiple collections, use the set & operator:

python
set1 = {"apple", "banana", "cherry"} set2 = {"cherry", "date", "fig"} common_fruits = set1 & set2 print(common_fruits) # Output: {'cherry'}

To clear out unwanted elements to filter data, use the - operator:

python
all_students = {"Alice", "Bob", "Charlie", "David"} passed_students = {"Alice", "David"} failed_students = all_students - passed_students print(failed_students) # Output: {'Bob', 'Charlie'}

Sets Vs. Other Data Structures

It is important to understand when to use a set instead of another data structure like dictionaries, lists and tuples. Here are the key differences between sets, dictionaries, lists, and tuples:

  • Sets: Use when you need an unordered collection of unique items.
  • Dictionaries: Use when you need to associate keys with values, and especially if you need fast lookups
  • Lists: Use when you need an ordered collection of items that can you modify and where the order of the elements is important
  • Tuples: Use when you need an unordered collection of items that should not change

Use the following links to learn more about each data structure in detail: Lists, Tuples, Dictionaries.

Conclusion

Sets are a powerful and efficient data structure in Python. Sets offer unique advantages when dealing with collections of unique elements and performing mathematical operations. Understanding how and when to use sets in Python will make you a skilled programmer, especially when working on data structures and algorithm problems.