Sets

Python 3.12 · ✓ verified by execution on 2026-07-23

A set is an unordered collection with no duplicate elements.

python
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(sorted(basket))
Output
['apple', 'banana', 'orange', 'pear']

Creating Sets

Curly braces or the set() function can be used to create sets.

python
print(set('abracadabra') == {'a', 'r', 'b', 'c', 'd'})
Output
True

The Empty Set Gotcha

Note: to create an empty set you have to use set(), not ; the latter creates an empty dictionary

python
empty_set = set()
empty_dict = {}
print(type(empty_set))
print(type(empty_dict))
Output
<class 'set'>
<class 'dict'>

Sets are Unordered

Because sets are unordered, iterating over them or printing them can produce the elements in a different order than you expect.

python
my_set = {'apple', 'banana', 'cherry'}
print(my_set[0])
This example raises an error (on purpose)
TypeError

Adding Elements

python
s = {'apple'}
s.add('banana')
print(sorted(s))
Output
['apple', 'banana']

Set Mathematics

Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

python · visualize
a = {'a', 'r', 'b', 'c', 'd'}
b = {'a', 'l', 'c', 'z', 'm'}
print(sorted(a - b))

Check yourself

You can create an empty set using {}.

Reveal answer

{} creates an empty dictionary. You must use set(). — {} creates an empty dictionary. You must use set().

Sets keep their items in the order you inserted them.

Reveal answer

Sets are completely unordered collections. — Sets are completely unordered collections.

You can access the first item in a set using my_set[0].

Reveal answer

Sets are not sequences; they do not support indexing. — Sets are not sequences; they do not support indexing.

Sets can contain lists or dictionaries.

Reveal answer

Set elements must be hashable, meaning they cannot be mutable objects like lists or dictionaries. — Set elements must be hashable, meaning they cannot be mutable objects like lists or dictionaries.

Challenges

Challenge 1 +15 XP

Write a function 'unique_names' that takes a list of names and returns a new list with all duplicates removed, sorted alphabetically.

python
  • Test 1 — expects "['Alice', 'Bob', 'Charlie'] \n"
Need a hint? (−25% XP)

Convert the list to a set to remove duplicates, then back to a list, and finally use sorted().

Show solution (0 XP)
def unique_names(names):
    return sorted(list(set(names)))
print(unique_names(['Alice', 'Bob', 'Alice', 'Charlie']))

Challenge 2 +15 XP

Write a function 'common_interests' that takes two lists of hobbies and returns a sorted list of the hobbies present in both.

python
  • Test 1 — expects "['coding', 'hiking'] \n"
Need a hint? (−25% XP)

Convert both lists to sets, use the & operator to find the intersection, then return a sorted list.

Show solution (0 XP)
def common_interests(list1, list2):
    return sorted(list(set(list1) & set(list2)))
print(common_interests(['reading', 'coding', 'hiking'], ['hiking', 'swimming', 'coding']))