HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
dictionary {}get()keys() / values() / items()set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›REFERENCE›Dictionary & Set
🗂️

Dictionary & Set

Key-value mappings (dict) and unique collections (set).

CommandDescription
dictionary {}Stores key-value pairs.
get()Safely gets a key's value.
keys() / values() / items()Iterates over the keys, values or pairs.
setA collection of unique (duplicate-free) values.

dictionary {}

Try it yourself →

Stores key-value pairs.

Syntax

name = {"key": value}

Description

A dictionary is created with curly braces. A value is accessed via its key (not an index). It's like 'labeled boxes' in the real world.

Returns

A dictionary object.

Example

person = {"name": "Alex", "age": 36}
print(person["name"])
person["city"] = "London"
print(person)

Expected output

Alex
{'name': 'Alex', 'age': 36, 'city': 'London'}

When to use?

Holding an object's attributes (a user, a product); JSON data.

Common mistakes

  • ⚠️ Accessing a missing key raises an error; use .get() for safe access.

get()

Try it yourself →

Safely gets a key's value.

Syntax

dict.get(key, default)

Description

If the key exists it returns its value; otherwise it returns the default you give (or None). Unlike square brackets, it doesn't raise an error.

Returns

The value or the default.

Example

k = {"name": "Alex"}
print(k.get("name"))
print(k.get("age", 0))

Expected output

Alex
0

When to use?

Reading fields that might be missing without crashing (settings, API data).

keys() / values() / items()

Try it yourself →

Iterates over the keys, values or pairs.

Syntax

dict.keys() · .values() · .items()

Description

keys() gives all keys, values() gives all values. items() gives (key, value) pairs — very commonly used in loops.

Example

k = {"a": 1, "b": 2}
for key, value in k.items():
    print(key, value)

Expected output

a 1
b 2

When to use?

Processing all of a dictionary's contents with a loop.

set

Try it yourself →

A collection of unique (duplicate-free) values.

Syntax

name = {a, b, c}

Description

A set holds each value once; it drops duplicates automatically. It's unordered and not indexed. Elements are added with add().

Returns

A set object.

Example

colors = {"red", "purple", "red"}
print(len(colors))
colors.add("green")
print("purple" in colors)

Expected output

2
True

When to use?

Removing duplicates, checking 'have I seen this value before'.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code