Sharing is caring 🙂

A set is a collection of unique items. Unlike lists and tuples, sets do not maintain any order, and items in a set cannot be accessed by their index. Sets are created using curly braces {} or the set() constructor, and items are separated by commas. Sets are useful for performing mathematical set operations such as union, intersection, and difference and quickly checking for membership in a collection of items. In this tutorial, we will look deeper into Python sets, including how to create and initialize them, modify them, and perform set operations and methods on them.

Understanding the Properties of Sets

Sets have a few key properties that make them different from other collection data types in Python such as lists and tuples. These properties include:

  1. Uniqueness: Sets only contain unique items. If an item is added to a set that already exists in the set, it will not be added again.
  2. Unordered: Sets do not maintain any order. The items in a set are not indexed, and their position in the set is not maintained.
  3. Mutable: Sets are mutable, meaning that they can be modified after they are created. Items can be added, removed, or changed in a set.
  4. Hashing: Sets use a process called hashing to quickly check for the membership of an item in the set. This makes the time complexity of membership checking O(1) on average.
  5. Immutable elements: The elements of the set must be immutable. This means that you cannot add lists, dicts, or other sets to a set.

You can see how sets can be useful in certain situations, such as quickly checking for membership in a large collection of items, or performing mathematical set operations.

Creating and Initializing Sets

There are several ways to create and initialize sets in Python. The most common way is to use curly braces {} and separate the items with commas. For example:

# Creating a set with curly braces
my_set = {1, 2, 3, 4}
print(my_set) 
# prints {1, 2, 3, 4}

You can also use the set() constructor to create an empty set, or to create a set from an iterable such as a list or tuple.

# Creating an empty set with the set() constructor
my_set = set()

# Creating a set from a list
my_list = [1, 2, 3, 4]
my_set = set(my_list)
print(my_set) 
# prints {1, 2, 3, 4}

You can also use a set comprehension to create a set.

# Creating a set using a comprehension
my_set = {i*2 for i in range(5)}
print(my_set) 
# prints {0, 2, 4, 6, 8}

It’s worth noting that, Sets always contains unique elements, so if you try to add an element that is already in the set, it will not be added again.

Modifying Sets

Sets are mutable, which means that items can be added, removed, or changed after the set is created. Here are some common methods for modifying sets in Python:

  1. add(): This method is used to add an item to a set.
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # prints {1, 2, 3, 4}
  1. update(): This method is used to add multiple items to a set at once. It can take an iterable such as a list or tuple as an argument.
my_set = {1, 2, 3}
my_set.update([4, 5, 6])
print(my_set) 
# prints {1, 2, 3, 4, 5, 6}
  1. remove() and discard(): These methods are used to remove an item from a set. The difference between them is that if the item to be removed is not in the set, remove() will raise an error while discard() will not.
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set) # prints {1, 2, 4}

my_set.discard(5) # will not raise an error
  1. pop(): This method is used to remove an arbitrary item from a set. The item removed is returned.
my_set = {1, 2, 3, 4}
item = my_set.pop()
print(item) # prints 1
print(my_set) # prints {2, 3, 4}
  1. clear(): This method is used to remove all items from a set.
my_set = {1, 2, 3, 4}
my_set.clear()
print(my_set) 
# prints set()

If you try to remove an item that is not in the set, the remove() method will raise an error while the discard() method will not. Also, the pop() method will raise an error if the set is empty.

Set Operations

Sets support several mathematical set operations, which can be performed using methods or operators. Here are some common set operations:

  1. Union: This operation returns a new set that contains all the items from both sets. It can be performed using the union() method or the | operator.
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Using the union() method
result = set1.union(set2)
print(result) # prints {1, 2, 3, 4, 5}

# Using the | operator
result = set1 | set2
print(result) # prints {1, 2, 3, 4, 5}
  1. Intersection: This operation returns a new set that contains only the items that are present in both sets. It can be performed using the intersection() method or the & operator.
set1 = {1, 2, 3}
set2 = {2, 3, 4}

# Using the intersection() method
result = set1.intersection(set2)
print(result) # prints {2, 3}

# Using the & operator
result = set1 & set2
print(result) # prints {2, 3}
  1. Difference: This operation returns a new set that contains the items that are present in the first set but not in the second set. It can be performed using the difference() method or the – operator.
set1 = {1, 2, 3}
set2 = {2, 3, 4}

# Using the difference() method
result = set1.difference(set2)
print(result) # prints {1}

# Using the - operator
result = set1 - set2
print(result) # prints {1}
  1. Symmetric Difference: This operation returns a new set that contains the items that are present in either set but not in both sets. It can be performed using the symmetric_difference() method or the ^ operator.
set1 = {1, 2, 3}
set2 = {2, 3, 4}

# Using the symmetric_difference() method
result = set1.symmetric_difference(set2)
print(result) # prints {1, 4}

# Using the ^ operator
result = set1 ^ set2
print(result) # prints {1, 4}

It’s worth noting that all these set operations will return a new set and won’t modify the original sets. Also, all these operations can be used with the assignment operator |=, &=, -=, ^= which will update the original set.

Set Methods

In addition to the set operations, sets also have several built-in methods that can be used to perform various actions. Here are some common set methods:

  1. copy(): This method returns a shallow copy of the set.
my_set = {1, 2, 3}
new_set = my_set.copy()
print(new_set) # prints {1, 2, 3}
  1. len(): This method returns the number of items in the set.
my_set = {1, 2, 3}
print(len(my_set)) # prints 3
  1. in: This method returns True if the specified item is present in the set, otherwise False.
my_set = {1, 2, 3}
print(2 in my_set) # prints True
print(4 in my_set) # prints False
  1. max() and min(): These methods return the maximum and minimum item in the set respectively. It’s worth noting that these methods only work for sets that contain numbers.
my_set = {1, 2, 3, 4}
print(max(my_set)) # prints 4
print(min(my_set)) # prints 1
  1. isdisjoint(): This method returns True if the set has no items in common with another set, otherwise False.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(set1.isdisjoint(set2)) # prints True
  1. issubset() and issuperset(): These methods check if the set is a subset or superset of another set respectively.
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1.issubset(set2)) # prints True
print(set2.issuperset(set1)) # prints True

These are some of the most common set methods, but there are others that can be used for specific tasks. All these methods will return a value and will not modify the original set.

Iterating Over Sets

Sets can be iterated over just like lists and tuples. The items in a set are unordered, so the order in which they are returned may not be the same as the order in which they were added to the set. Here are a couple of ways to iterate over a set:

  1. Using a for loop:
my_set = {1, 2, 3, 4}
for item in my_set:
    print(item)
  1. Using the set’s iterator:
my_set = {1, 2, 3, 4}
it = iter(my_set)
print(next(it))
print(next(it))

Sets are unordered, so the order of items may not be the same as the order in which they were added to the set. If you need to iterate over a set and maintain order, you can convert it to a list or tuple before iterating.

The set is a mutable data structure, so if you are iterating over the set and try to add or remove an element from the set, it will raise an error. To avoid this, you can create a copy of the set or use the iterator which will not raise an error, but it will not iterate over the new elements that were added or removed.

Set Comprehension

Set comprehension is a concise way to create a set in Python. It is similar to list comprehension, but it creates a set instead of a list. The syntax for set comprehension is similar to that of list comprehension, but with curly braces {} instead of square brackets []. Here is an example of set comprehension:

# Using a for loop and add() method to create a set
my_list = [1, 2, 3, 4, 5]
my_set = set()
for i in my_list:
    if i % 2 == 0:
        my_set.add(i)

# Using set comprehension to create the same set
my_set = {i for i in my_list if i % 2 == 0}
print(my_set) # prints {2, 4}

Set comprehension can also be used to create sets from other sets, lists, or tuples. Here is an example of creating a set from another set:

set1 = {1, 2, 3, 4}
set2 = {i*2 for i in set1}
print(set2) # prints {2, 4, 6, 8}

Set comprehension can’t be used to create a set from a dictionary, because dictionaries are not iterable. Set comprehension is a powerful and concise way to create sets, and can make your code more readable and efficient when working with sets.

Frozen Sets

Frozen sets are similar to sets, but they are immutable, meaning that they cannot be modified after they are created. Frozen sets are created using the frozenset() constructor or by using a set comprehension with parentheses instead of curly braces. Here is an example of creating a frozen set:

# Using the frozenset() constructor
my_frozenset = frozenset([1, 2, 3, 4])

# Using set comprehension with parentheses
my_frozenset = frozenset(i*2 for i in range(5))

Because frozen sets are immutable, they do not have methods for adding or removing items, and trying to do so will raise an error. However, they can still be iterated over, and they support all the mathematical set operations such as union, intersection, and difference. Frozen sets can be useful in situations where you need to use a set as a key in a dictionary, or as an element in another set, because only immutable objects can be used as keys or elements.

Frozen sets are hashable, meaning that they can be used as a key in a dictionary or as an item in another set. And, unlike sets, frozen sets are thread-safe, which means that they can be safely used in multi-threaded programs.

Real-World Applications of Sets in Python

Here are some common real-world applications of sets:

  1. Removing duplicates: Sets can be used to remove duplicates from a list or other iterable. By converting the iterable to a set and then back to a list, only the unique items will remain.
my_list = [1, 2, 3, 4, 4, 4, 5, 5]
my_list = list(set(my_list))
  1. Membership testing: Sets are optimized for membership testing, meaning that checking if an item is in a set is much faster than checking if an item is in a list or tuple. This is because sets use a process called hashing to quickly check for membership.
  2. Set operations: Sets support several mathematical set operations such as union, intersection, and difference. These operations can be useful for working with sets of items in a variety of applications such as finding common items between sets, or finding items that are unique to a set.
  3. Dictionary keys: Sets are immutable, so they can be used as keys in a dictionary, while lists and tuples cannot.
  4. Removing items while iterating: Since sets are mutable, you can remove items while iterating over the set. This can be useful in situations where you need to remove items that meet certain criteria.
  5. In web scraping, Sets can be used to store unique URLs that have been visited, so that we don’t visit the same page again.
  6. In data analysis, Sets can be used to find unique values in a dataset and to remove outliers.

Sets can be a versatile data structure that can help you write more efficient, readable, and maintainable code

Python Sets FAQ

Q: What is a set in Python? A: A set is a collection data type in Python that stores unique items in an unordered way. Sets use a process called hashing to quickly check for membership, making it faster than checking if an item is in a list or tuple.

Q: How do I create a set in Python? A: Sets can be created using curly braces {} and separating the items with commas, using the set() constructor, or using set comprehension.

Q: What are the properties of sets in Python? A: Sets have several properties that make them different from other collection data types in Python. These properties include: uniqueness, unordered, mutability, hashing, and immutable elements.

Q: How do I add or remove items from a set in Python? A: Sets are mutable, so items can be added or removed using methods such as add(), update(), remove(), and discard() or pop().

Q: How do I perform set operations in Python? A: Sets support several mathematical set operations such as union, intersection, and difference. These operations can be performed using methods such as union(), intersection(), and difference() or using operators such as |, &, and -.

Q: How do I iterate over a set in Python? A: Sets can be iterated over using a for loop or by using the set’s iterator. However, it’s worth noting that since sets are unordered, the order in which the items are returned may not be the same as the order in which they were added to the set.

Q: Can I use sets as keys in a dictionary in Python? A: Yes, sets are immutable, so they can be used as keys in a dictionary. But lists and tuples cannot be used as keys because they are mutable.

Q: What is the difference between a set and a frozenset in Python? A: Sets are mutable, meaning that items can be added, removed, or changed after the set is created. On the other hand, frozensets are immutable, meaning that they cannot be modified after they are created.

Q: What are the real-world applications of sets in Python? A: Sets have many real-world applications such as removing duplicates from a list, membership testing, set operations, using them as keys in a dictionary, removing items while iterating, in web scraping, in data analysis and many others.

Q: Can a set contain multiple data types? A: No, all the items in a set must be of the same data type. But, a set can contain elements of different data types if they are all immutable.

Sharing is caring 🙂