LambdaLabTM
Computer Science · Class 11 · Data Types
Data TypesExtra⏱️ 7 min read

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.

Note
This one is a bonus. Sets are not part of the CBSE Class 11 syllabus, so nothing in an exam will ask you for one. They are here because they finish the family tree, and because the one thing they do — throwing away repeats — is genuinely useful and takes about five minutes to learn.

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.

a_set.py
numbers = {10, 20, 30}

print(numbers)
print(type(numbers))
print(len(numbers))
print(20 in numbers)
Output
{10, 20, 30}
<class 'set'>
3
True

So 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:

no_repeats.py
marks = {10, 20, 20, 30, 10}

print(marks)
print(len(marks))
Output
{10, 20, 30}
3

Five 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.

from_a_list.py
numbers = [3, 1, 2, 1, 3, 3]
unique = set(numbers)

print(numbers)
print(unique)
print(len(numbers), len(unique))
Output
[3, 1, 2, 1, 3, 3]
{1, 2, 3}
6 3

The 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:

from_a_tuple.py
print(set((5, 5, 7)))
Output
{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:

from_a_string.py
word = 'hello'
letters = set(word)

print(len(word))
print(len(letters))
print(sorted(letters))
Output
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:

no_positions.py
numbers = {10, 20, 30}

print(numbers[0])
Output
Traceback (most recent call last):
  File "no_positions.py", line 3, in <module>
    print(numbers[0])
          ~~~~~~~^^^
TypeError: 'set' object is not subscriptable
Watch Out
Empty curly braces are a dictionary, not a set. Python had to give { } to one of the two, and the dictionary got there first. So an empty set has to be written set():
empty.py
a = {}
b = set()

print(type(a))
print(type(b))
Output
<class 'dict'>
<class 'set'>

5Try it

playing.py

6Recap

Key Takeaway
A set is values in curly braces, and it cannot hold the same value twice — repeats are dropped without a word. 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.
ListSet
Written with[ ]{ }
Same value twice?allowedimpossible
Keeps the order?yesno
Indexing & slicingyesno
Can be changed?yesyes
Quick Check

What does {10, 20, 20, 30} print?

Quick Check

What is len(set([3, 1, 2, 1, 3, 3]))?

Quick Check

What is type({})?