Using curly braces
Imagine you‘re a data scientist analyzing results from a survey where participants could select multiple options. To easily find the distinct choices selected across all responses, you could use a Python set.
Sets are a versatile and powerful data structure in Python that every data science professional should have in their toolkit. In this beginner-friendly guide, we‘ll dive into what sets are, when to use them, and key operations you can perform. By the end, you‘ll have a solid grasp on sets and be ready to wield them in your data science projects.
What are Sets in Python?
A set is an unordered collection of unique elements enclosed in curly braces {}. The key characteristics of Python sets are:
- Elements are unordered and their position can change
- Elements must be unique – duplicates are not allowed
- Elements must be immutable (unchangeable) – you can‘t include lists or dictionaries in a set
- Sets themselves are mutable – you can add or remove elements
This makes sets ideal for situations where you want to:
- Store distinct values and avoid duplicates
- Check for membership ("is this value in the set?") efficiently
- Perform mathematical set operations like unions and intersections
In contrast, lists and tuples are ordered sequences that allow duplicates. Lists are mutable while tuples are immutable. Dictionaries are unordered mappings of unique keys to values.
Here‘s what a set looks like in Python:
fruit = {‘apple‘, ‘banana‘, ‘orange‘}
Creating Sets in Python
There are two ways to create a set in Python:
- Using curly braces {} and separating elements with commas
- Using the set() constructor function and passing in an iterable like a list or tuple
colors = {‘red‘, ‘green‘, ‘blue‘}
numbers = set([1, 2, 3, 4, 5])
If you pass in a string to set(), it will split it into a set of characters:
letters = set(‘hello‘)
print(letters) # {‘h‘, ‘e‘, ‘l‘, ‘o‘}
To create an empty set, you must use set(). Using empty curly braces {} will create an empty dictionary instead.
empty_set = set()
Adding and Removing Elements
You can add single elements to a set using the add() method and multiple elements using update():
fruit = {‘apple‘, ‘banana‘}
fruit.add(‘orange‘)
print(fruit) # {‘orange‘, ‘banana‘, ‘apple‘}
fruit.update([‘grapes‘, ‘mango‘])
print(fruit) # {‘orange‘, ‘mango‘, ‘grapes‘, ‘banana‘, ‘apple‘}
To remove elements, you have a few options:
- remove(x) – removes element x, raises a KeyError if it doesn‘t exist
- discard(x) – removes element x if it‘s present, does nothing if it doesn‘t exist
- pop() – removes and returns an arbitrary element, raises a KeyError if the set is empty
- clear() – removes all elements from the set
fruit = {‘apple‘, ‘banana‘, ‘orange‘}
fruit.remove(‘banana‘)
print(fruit) # {‘orange‘, ‘apple‘}
fruit.discard(‘grapes‘) # no error
print(fruit) # {‘orange‘, ‘apple‘}
item = fruit.pop()
print(item) # orange (could be apple)
fruit.clear()
print(fruit) # set()
Membership Tests
To check if an element exists in a set, use the in keyword. This is much faster than checking membership in a list, since sets use hashing to enable O(1) lookups on average.
fruit = {‘apple‘, ‘banana‘, ‘orange‘}
print(‘apple‘ in fruit) # True
print(‘grapes‘ in fruit) # False
Iterating Over a Set
You can iterate over the elements of a set using a for loop:
colors = {‘red‘, ‘green‘, ‘blue‘}
for color in colors:
print(color)
Note that the order of elements printed may vary each time, since sets are unordered by definition.
Mathematical Set Operations
Python provides operators and methods to perform common mathematical set operations:
- Union – elements that exist in either set
— | operator or union() method - Intersection – elements that exist in both sets
— & operator or intersection() method - Difference – elements that exist in the first set but not the second
— – operator or difference() method - Symmetric Difference – elements that exist in either set but not both
— ^ operator or symmetric_difference() method
Here‘s an example showing each operation:
A = {1, 2, 3, 4}
B = {2, 4, 6, 8}
print(A | B) # {1, 2, 3, 4, 6, 8}
print(A.union(B)) # {1, 2, 3, 4, 6, 8}
print(A & B) # {2, 4}
print(A.intersection(B)) # {2, 4}
print(A - B) # {1, 3}
print(A.difference(B)) # {1, 3}
print(A ^ B) # {1, 3, 6, 8}
print(A.symmetric_difference(B)) # {1, 3, 6, 8}

Frozen Sets
Python also provides an immutable version of sets called frozenset. Once created, a frozenset cannot be changed – you can‘t add or remove elements.
To create a frozenset, use the frozenset() constructor function:
numbers = frozenset([1, 2, 3, 4])
print(numbers) # frozenset({1, 2, 3, 4})
Frozensets support all the same operations as regular sets except those that modify the set (add, remove, etc.). They are hashable, so they can be used as dictionary keys or elements of other sets.
grades = {frozenset([‘A‘, ‘B‘]): ‘Pass‘, frozenset([‘C‘, ‘D‘]): ‘Fail‘}
Putting It All Together
Let‘s walk through an example of using sets to analyze survey responses. Suppose we have the following list of responses, where each respondent could select multiple colors:
responses = [
[‘red‘, ‘green‘],
[‘blue‘],
[‘green‘, ‘blue‘],
[‘red‘, ‘blue‘],
[‘red‘, ‘green‘, ‘blue‘]
]
To find the distinct colors selected across all responses, we can iterate through the list and union the sets together:
distinct_colors = set()
for response in responses:
distinct_colors = distinct_colors | set(response)
print(distinct_colors) # {‘red‘, ‘green‘, ‘blue‘}
We can also count how many respondents selected each color by initializing a dictionary with the distinct colors and incrementing counts:
color_counts = {color: 0 for color in distinct_colors}
for response in responses:
for color in set(response):
color_counts[color] += 1
print(color_counts) # {‘red‘: 3, ‘green‘: 3, ‘blue‘: 4}
With just a few lines of code using sets, we quickly extracted insights from our survey data. This is a simple example, but it demonstrates the power and flexibility of sets for data analysis tasks.
Key Takeaways
- Sets are unordered collections of unique elements useful for storing distinct values, membership checking, and mathematical set operations
- Create sets in Python using {} or set() and add/remove elements with methods like add(), update(), remove(), and clear()
- Leverage efficient membership tests using the in keyword
- Use operators and methods to perform union, intersection, difference, and symmetric difference of sets
- Opt for frozenset if you need an immutable set or want to use it as a dictionary key
Understanding when and how to use sets is an essential skill for data scientists. Sets provide a clean, efficient way to handle unique values and perform set operations. This comes in handy for tasks like finding distinct elements, comparing groups, and analyzing relationships between data.
Put Your Knowledge Into Practice
Now that you‘ve learned the fundamentals of Python sets, it‘s time to start using them in your own code! Here are a few ideas:
- Analyze a dataset and use sets to find the distinct values in each column
- Write a function that takes two sets as input and returns their union, intersection, and difference
- Use a set to efficiently check for duplicate entries in a list
- Try using a frozenset as a dictionary key or element in another set
As you encounter coding problems and data cleaning tasks, consider if a set might offer a more elegant solution than a list or dictionary. With practice, you‘ll start reaching for sets naturally and discover even more scenarios where they streamline your workflow.
Becoming proficient with sets is just one step in your journey to becoming a data science professional. For more guides on essential Python concepts and skills, check out the following resources:
- Sets in Python Tutorial from DataCamp
- Sets in Python from Real Python
- Difference between List, Tuple, Set and Dictionary in Python from Analytics Vidhya
Happy coding and analyzing!