Skip to content
BENIBIX Learn

Dictionary vs Set

People think a set is just a dictionary with no values, because in Python both are written with curly brackets. A dictionary pairs each key with a value, so you look a value up by its key. A set holds single values, each one once, and has nothing to look up. In Python, {} on its own makes an empty dictionary. An empty set is written set().

Development🧩 Programming Concepts

Dictionary

A collection of key and value pairs. You look a value up by its key.

A dictionary is a collection that stores pairs: a key and a value. You look a value up by its key, not by a number. Each key appears only once, like each word in a real dictionary. Python calls it a dict, JavaScript uses an object or a Map, and many others call it a map. It is the fastest way to find one thing by its name.

Example
A class app keeps ages as {"Emma": 36, "Jack": 12}. Asking for "Jack" gives back 12 at once.
Why it matters
Settings, user records and counts are all looked up by name. A dictionary finds them without searching the whole list.
Full entry
Development🧩 Programming Concepts

Set

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

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
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.
Full entry

In one line

Dictionary
A collection of key and value pairs. You look a value up by its key.
Set
A group of values with no repeats. Each value is in it just once.