Sets
You have seen the name set twice already — once on the family tree, and once in the mutable column. Here it is properly. A set is a collection much like a list, with one rule that changes everything: it cannot hold the same value twice.
1Values in curly braces
Write the values inside curly braces { }, separated by commas — the same punctuation a dictionary uses, but with plain values instead of key: value pairs.
numbers = {10, 20, 30}
print(numbers)
print(type(numbers))
print(len(numbers))
print(20 in numbers){10, 20, 30}
<class 'set'>
3
TrueSo far it behaves much as a list would: len() counts the items, and in checks whether something is inside. The difference shows up the moment a value repeats.
2Repeats simply vanish
Write the same value twice and Python does not complain. It just keeps one of it:
marks = {10, 20, 20, 30, 10}
print(marks)
print(len(marks)){10, 20, 30}
3Five values went in and three came out. Nothing was reported, because nothing went wrong — a set is defined as a collection with no duplicates, so removing them is not a correction, it is simply what a set is. Compare that with a list, where [10, 20, 20, 30, 10] keeps all five happily.
3set(): the useful part
This is what sets are actually for at your stage. set() is a function like list() and tuple(): hand it a list, a tuple or a string, and it gives you back a set of the same items — which means with every repeat gone.
numbers = [3, 1, 2, 1, 3, 3]
unique = set(numbers)
print(numbers)
print(unique)
print(len(numbers), len(unique))[3, 1, 2, 1, 3, 3]
{1, 2, 3}
6 3The original list is untouched — set() built a new thing, in the same way sorted() did. Six items became three, and you did not have to check anything by hand.
A tuple works the same way:
print(set((5, 5, 7))){5, 7}And a string is the interesting one, because a string is a sequence of characters — so you get the set of letters it uses:
word = 'hello'
letters = set(word)
print(len(word))
print(len(letters))
print(sorted(letters))5 4 ['e', 'h', 'l', 'o']
Five characters, four different letters — the second l vanished. That is the whole idea in one line: how many different things are in here?
4A set has no order, so it has no positions
The example above printed sorted(letters) rather than letters, and that was deliberate. A set does not remember the order you wrote things in, so Python is free to show them in whatever order it likes — and for letters it may genuinely differ from one run to the next.
No order means no positions, and no positions means no indexing and no slicing. This is the one place a set is clearly less than a list:
numbers = {10, 20, 30}
print(numbers[0])Traceback (most recent call last):
File "no_positions.py", line 3, in <module>
print(numbers[0])
~~~~~~~^^^
TypeError: 'set' object is not subscriptable{ } to one of the two, and the dictionary got there first. So an empty set has to be written set():a = {}
b = set()
print(type(a))
print(type(b))<class 'dict'> <class 'set'>
5Try it
6Recap
set() turns a list, tuple or string into one, which is the quick way to ask how many different items are in here? A set keeps no order, so there is no [0] and no slicing. And an empty set is set(), because { } already means an empty dictionary.| List | Set | |
|---|---|---|
| Written with | [ ] | { } |
| Same value twice? | allowed | impossible |
| Keeps the order? | yes | no |
| Indexing & slicing | yes | no |
| Can be changed? | yes | yes |
What does {10, 20, 20, 30} print?
What is len(set([3, 1, 2, 1, 3, 3]))?
What is type({})?