Skip to content
BENIBIX Learn

Set

Development🧩 Programming ConceptsData and Variables

Also known as Hash Set

A group of values with no repeats. Each value is in it just once.

Think of it like

Think of it like a sticker book. Each sticker goes in once, and a spare has no spot.

Example

A blog has the tags red, blue and red on one post. Put in a set, the tags become just red and blue.

Why it matters

Checking if something is in a set is much faster than searching a long list. It also clears out repeats for free.

Definition

A set is a collection where each value can appear only once. Add the same value twice and the set still holds one copy. Most sets keep no order, so there is no first or last item. Sets are quick at answering one question: is this value in here? They are handy for removing repeats from a list.

Example code

python
>>> tags = {"red", "blue", "red"}
>>> len(tags)
2
>>> "red" in tags
True
Red was added twice, but the set keeps one copy.

Common mistake

Expecting a set to keep its items in order. Use an array when the order matters.

Key takeaways

  1. 1
    No repeats. Each value is kept only once.
  2. 2
    No set order. Items have no first or last.
  3. 3
    Fast to check. Is it in here? is answered at once.

Compare

Learn it properly