top of page

Mastering Python Sets: A Comprehensive Guide

Python set is an unordered collection of unique elements. Sets are commonly used to store and manipulate collections of data in which the order of the elements is not important and duplicates are not allowed. Yes, read it right!

  • One of the main characteristics of sets is that they only allow unique elements. This means that if you try to add an element that is already in the set, it will not be added again. This makes sets a useful data structure for removing duplicates from a collection of data.

  • Sets are also mutable, which means that you can add, remove, and modify elements in a set. You can use the add(), union(), and remove() methods to add, combine, and remove elements from a set, respectively. You can also use the clear() method to remove all elements from a set.

Also, Python sets do not have an indexing system like lists and tuples, which means that you cannot access or modify elements using an index. However, you can use the in keyword to check if an element is in a set.



Python Sets

A set can be created by using the built-in set() function, or by using curly braces {} with elements separated by commas. For example, the following code creates an empty set:

test_set = set()

You can also create a set with initial elements by passing them as arguments to the set() function or placing them inside curly braces.

#to create a set
test_set = {'John', 'Lucy', 'Mike', 'Harry'}
print(test_set)

Output: {'Lucy', 'John', 'Mike', 'Harry'}

Basic Functions and Methods for Python Set

One can modify the contents of a set by using set methods such as add(). You can also delete items from a set using the remove() or clear() methods.

  • add(): the add() method adds the element to the set if it is not already present. If the element is already in the set, the add() method will not add it again. This is because sets only allow unique elements, and the add() method will not add a duplicate element to the set.

test_set.add('Ron')
print(test_set)

Output: {'Ron', 'Lucy', 'John', 'Mike', 'Harry'}

Some common types of elements that you might add to a set include integers, floats, strings, and tuples.

Keep in mind that the add() method does not return a new set. Instead, it modifies the existing set in place. If you want to create a new set with the added element, you can use the union() method.

  • union(): the union() method returns a new set that contains the elements from both sets. In other words, the union() method returns the set of elements that are present in either set.

#to create sets
test_set_x = {'John', 'Mike', 'Ron'}
test_set_y = {'Lucy', 'Harry'}

test_set = test_set_x.union(test_set_y)
print(test_set)  

Output: {'Ron', 'Lucy', 'John', 'Mike', 'Harry'}

#other direction
test_set = test_set_y.union(test_set_x)
print(test_set)

Output: {'Ron', 'Lucy', 'John', 'Mike', 'Harry'}

#to create sets with overlapping elements
test_set_x = {'John', 'Mike', 'Ron'}
test_set_y = {'Lucy', 'Harry', 'Mike'}

test_set = test_set_x.union(test_set_y)
print(test_set)

Output: {'Ron', 'Lucy', 'John', 'Mike', 'Harry'}

You can use the union() method to find the elements that are present in either set. This can be useful for combining sets or for identifying all elements in a set.

  • remove(): the remove() method removes the element from the set if it is present. If the element is not in the set, the remove() method will raise a KeyError exception.

test_set.remove('Ron')
print(test_set)

Output: {'John', 'Lucy', 'Mike', 'Harry'}

Some common types of elements that you might remove from a set include integers, floats, strings, and tuples.

It's also worth noting that the discard() method is similar to the remove() method, but it does not raise an exception if the element is not in the set. This can be useful if you want to remove an element from a set without checking if it is present.

  • clear(): the clear() method removes all elements from the set, leaving an empty set. It does not take any arguments and does not return a value.

test_set.clear()
print(test_set)

Output: set()

This can be useful if you want to reuse a set or if you want to reset a Python set to its initial state.


It's also worth noting that you can instead create an empty set using the set() function with clearing an existing one to make use of, like this:

test_set_z = set()

This creates a new empty set, which you can then add elements to using the add() method.

  • difference(): the difference() method returns a new set that contains the elements that are in the first set but not in the second set. In other words, the difference() method returns the set of elements that are present in the first set but absent in the second set.

test_set = test_set_x.difference(test_set_y)
print(test_set)

Output: {'John', 'Ron'}

test_set = test_set_y.difference(test_set_x)
print(test_set)

Output: {'Lucy', 'Harry'}

This can be useful for comparing sets or for identifying unique elements in a set.

  • intersection(): the intersection() method returns a new set that contains the elements that are present in both sets. In other words, the intersection() method returns the set of elements that are common to both sets.

test_set = test_set_x.intersection(test_set_y)
print(test_set)

Output: {'Mike'}

#other direction
test_set = test_set_y.intersection(test_set_x)
print(test_set)

Output: {'Mike'}

This can be useful for comparing sets or for identifying common elements in a set.


Use Cases of Python Sets

Sets are an important data structure in Python and are commonly used for various purposes, including:

  1. Removing duplicates: Because sets only allow unique elements, they are often used to remove duplicates from a collection of data. For example, you might use a set to remove duplicates from a list of names or a list of numbers.

  2. Membership testing: You can use the in keyword to check if an element is in a set, which makes sets an efficient data structure for membership testing. For example, you might use a set to check if a user has already visited a particular page on a website.

  3. Set operations: Python provides a number of set operations, such as union, intersection, and difference, which can be useful for manipulating sets. For example, you might use the union() method to combine two sets, or the intersection() method to find the elements that are common to both sets, or the difference() method to find the distinct elements.

  4. Dictionary keys: Sets are hashable, which means that they can be used as dictionary keys. This can be useful if you want to create a dictionary with a set of values as the keys.

160 views0 comments
bottom of page