Skip to content

Comprehensive Python Guide: Introduction Sets Unordered Collections Unique Elements

Updated: at 03:01 AM

A set in Python is an unordered collection of unique and immutable objects. Set elements must be hashable objects like integers, floats, tuples, frozensets, and strings. Sets do not allow duplicate elements and are not indexed like lists or tuples.

Sets are useful for mathematical operations like unions, intersections, differences, and symmetric differences. Checking membership in a set is very efficient (O(1) on average). Sets also remove duplicate entries from sequences and can compute differences and intersections between other sets.

In this comprehensive guide, we will cover the following topics related to sets in Python:

Table of Contents

Open Table of Contents

Set Properties and Characteristics

Sets in Python have the following properties:

Below is a simple example of creating a set with unique, unordered string elements:

# Create a set of strings
fruits = {'apple', 'banana', 'orange', 'grapefruit'}

print(fruits)
# {'grapefruit', 'orange', 'apple', 'banana'} - Order random

Key properties like unordered and unique elements make sets useful for removing duplicates and computing differences between other collections.

Creating Sets

There are a few different ways to create a set in Python:

set() Constructor

The built-in set() constructor can be used to create a set from an iterable object like a list or tuple. Duplicates are removed and elements are stored in a random, unordered fashion.

# Create set from a list
my_set = set([1, 2, 2, 3, 4])

print(my_set)
# {1, 2, 3, 4}

{} Curly Braces

Sets can also be created by enclosing elements inside curly braces {}.

# Create set with curly braces
my_set = {1, 4, 3, 3, 2}

print(my_set)
# {1, 2, 3, 4}

set Comprehensions

Set comprehensions provide a concise way to generate new sets using an expression.

# Create set with set comprehension
my_set = {x**2 for x in range(10)}

print(my_set)
# {0, 1, 64, 4, 36, 100, 9, 16, 49, 81}

Modifying Sets

We can modify sets by adding or removing elements. Keep in mind that set elements are immutable. We can also completely clear a set of all elements.

add()

Individual elements can be added to a set using the add() method. Adding duplicate elements is ignored and has no effect.

fruits = {'apple', 'banana', 'orange'}

fruits.add('grape')

print(fruits)
# {'apple', 'grape', 'banana', 'orange'}

update()

Multiple elements can be added to a set at once using the update() method. Duplicate elements are ignored.

fruits = {'apple', 'banana', 'orange'}

fruits.update(['grape', 'mango', 'apple'])

print(fruits)
# {'apple', 'mango', 'grape', 'orange', 'banana'}

remove()

The remove() method can remove a specific element from a set. If the element does not exist, a KeyError is raised.

fruits = {'apple', 'banana', 'orange'}

fruits.remove('banana')

print(fruits)
# {'apple', 'orange'}

discard()

The discard() method will remove an element from a set if it exists, but will NOT raise any errors.

fruits = {'apple', 'banana', 'orange'}

fruits.discard('grape') # No errors

print(fruits)
# {'apple', 'orange', 'banana'}

pop()

The pop() method removes and returns an arbitrary element from the set. Sets are unordered so we don’t know which element will be returned.

fruits = {'apple', 'banana', 'orange'}

item = fruits.pop()

print(item) # 'orange' (for example)
print(fruits) # {'apple', 'banana'}

clear()

The clear() method empties all elements from the set and returns an empty set.

fruits = {'apple', 'banana', 'orange'}

fruits.clear()

print(fruits)
# set()

Set Operations

Sets come with built-in methods to perform common mathematical set operations like unions, intersections, differences and symmetric differences.

Union

The union of two sets combines them and returns a new set containing all unique elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

set3 = set1.union(set2) # or set1 | set2

print(set3)
# {1, 2, 3, 4, 5}

Intersection

The intersection of two sets returns a new set containing only the common elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

set3 = set1.intersection(set2) # or set1 & set2

print(set3)
# {3}

Difference

The difference between two sets returns a new set containing only the elements from the first set that ARE NOT in the second set.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

set3 = set1.difference(set2) # or set1 - set2

print(set3)
# {1, 2}

Symmetric Difference

The symmetric difference returns a new set containing only the elements that are NOT shared between the sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

set3 = set1.symmetric_difference(set2) # or set1 ^ set2

print(set3)
# {1, 2, 4, 5}

Set Methods

Sets come with a variety of built-in methods that are useful for tasks like checking length, membership, retrieving max/min value, etc.

len()

Return the number of elements in the set.

fruits = {'apple', 'banana', 'orange'}

print(len(fruits))
# 3

max()/min()

Return the largest and smallest element in the set based on value.

nums = {5, 2, 7, 1}

print(max(nums)) # 7
print(min(nums)) # 1

sum()

Sum the elements of the set.

nums = {1, 5, 2}

print(sum(nums)) # 8

any()/all()

Check if any or all elements meet a condition.

nums = {1, 3, 2}

print(any(n > 1 for n in nums)) # True
print(all(n > 1 for n in nums)) # False

sorted()

Return a new sorted list from the set’s elements.

fruits = {'orange', 'grape', 'apple'}

print(sorted(fruits))
# ['apple', 'grape', 'orange']

Set Membership

We can check if an element exists in a set or does not exist using the in and not in operators. This is very efficient, O(1) on average.

in operator

fruits = {'apple', 'banana', 'orange'}

print('apple' in fruits) # True

not in operator

fruits = {'apple', 'banana', 'orange'}

print('grape' not in fruits) # True

Frozensets

A frozenset is an immutable or hashable set that cannot be changed after it is created. They can be used as dictionary keys or elements of another set.

fs = frozenset([1, 2, 3])

fs.add(4) # AttributeError

my_dict = {fs: 'hello'}
my_set = {fs, 1, 2}

Frozensets support all the standard set operations like unions, intersections, etc. But since they immutable, the result is returned as a new frozenset.

Use Cases for Sets

Here are some common use cases where sets are useful in Python:

By leveraging properties like uniqueness and mathematical operations, sets are an indispensable built-in data type in Python.

Conclusion

Sets are unordered collections of unique, immutable objects in Python. Elements must be hashable and sets do not allow duplicate entries. Sets are useful for tasks like membership testing, eliminating duplicates, and computing mathematical set operations like unions, intersections, and differences.

In this comprehensive guide, we looked at how to create sets using the set() constructor, curly braces, and set comprehensions. We covered modifying sets by adding and removing elements as well as common set operations and methods. We also explored set membership testing and use cases for sets like removing duplicates and store unique elements.

Sets are a fundamental built-in data type in Python and have many applications from text analysis to data science. Their high-performance mathematical operations and ability to remove duplicates from sequences make them an important tool for every Python programmer.

I hope this guide provides a helpful and thorough introduction to sets in Python. Let me know if you have any other questions!