Set


Collection Type Ordered Mutable Duplicate Elements Indexing Example
Set No Yes No No {"a", "b", 23}


A set is a collection that is unordered and unchangeable* (in reality you can still add and remove elements). It does not allows to duplicate members.

# To create (instantiate)
a = {"a", "b", 23}  # cannot have doubles!

# To retrieve an element
impossible!  # only with list(a)[i]

# To remove an element
a.remove(beam_12)

# To insert an element
a.append(post_01)


🦊 set are often use for unique values. For example, if you want to find all the unique words in a text. You can use a set to do this. First, you would split the text into words. Then, you would add each word to a set. Since sets can only contain unique values, at the end you would have a set containing all the unique words in the text. This feature can be very handy! 🦊

🛠 Exercise


🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍

Solution:

🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍