CodingNic

Data Structures

Dictionaries

Data Structures 26 min read

Dictionaries

Dictionaries

Sometimes data belongs in pairs.

For example:

  • A name and phone number
  • A product and price
  • A country and capital
  • A username and password

Python gives us dictionaries for this.

What Is a Dictionary?

A dictionary stores data as key: value pairs.

Think of a real dictionary:

  • Word = key
  • Meaning = value

In Python:

  • "name" = key
  • "Tom" = value

Dictionaries use curly brackets {}.

python
user = {
    "name": "Tom",
    "age": 25
}

print(user)

Output

text
{'name': 'Tom', 'age': 25}

Why Dictionaries Matter

Dictionaries help you:

  • Store related data clearly
  • Find values by name
  • Update values easily
  • Add new data
  • Remove data quickly

Creating a Dictionary

python
student = {
    "name": "Sara",
    "score": 90,
    "passed": True
}

Reading Values

Use the key name.

python
student = {
    "name": "Sara",
    "score": 90
}

print(student["name"])
print(student["score"])

Output

text
Sara
90

Using get()

get() is a safe way to read a key.

python
print(student.get("name"))
print(student.get("grade"))

Output

text
Sara
None

If the key does not exist, get() returns None.

Updating Values

python
student["score"] = 95
print(student)

Output

text
{'name': 'Sara', 'score': 95}

Adding New Items

Use a new key.

python
student["city"] = "Toronto"
print(student)

Output

text
{'name': 'Sara', 'score': 95, 'city': 'Toronto'}

Removing Items with pop()

python
student.pop("city")
print(student)

Output

text
{'name': 'Sara', 'score': 95}

Removing Last Item with popitem()

python
student.popitem()
print(student)

Output

text
{'name': 'Sara'}

Clearing All Items

python
student.clear()
print(student)

Output

text
{}

Dictionary Length

Use len().

python
data = {"a": 1, "b": 2, "c": 3}
print(len(data))

Output

text
3

Loop Through Keys

python
user = {"name": "Tom", "age": 25}

for key in user:
    print(key)

Output

text
name
age

Loop Through Values

python
for value in user.values():
    print(value)

Output

text
Tom
25

Loop Through Keys and Values

python
for key, value in user.items():
    print(key, value)

Output

text
name Tom
age 25

Code Along

python
product = {
    "name": "Laptop",
    "price": 900
}

product["stock"] = 5

for key, value in product.items():
    print(key, value)

Output

text
name Laptop
price 900
stock 5

Mini Challenge

Build a profile dictionary.

Steps:

  • Create a dictionary with:
    name, age, city
  • Print the name
  • Update the age
  • Add job
  • Remove city
  • Print the final dictionary

Expected output:

text
Tom
{'name': 'Tom', 'age': 26, 'job': 'Designer'}

Build a student manager.

Steps:

  • Create a dictionary with:

    • name: Tom
    • age: 25
    • city: New York
  • Print the value of name

  • Update age to 26

  • Add a new key called course with value Python

  • Print the value of course

  • Remove city

  • Print all keys and values using a loop

  • Print the final dictionary

Expected output:

text
Tom
Python
name Tom
age 26
course Python
{'name': 'Tom', 'age': 26, 'course': 'Python'}

Real World Use Case

Programs use dictionaries for user accounts, product data, settings, APIs, and storing records with labels.

Quiz

  1. What does a dictionary store?
  2. Which brackets are used for dictionaries?
  3. What does get() do?
  4. Which method loops through key and value pairs?

Assignment

Create a dictionary for a book with title, price, and author. Update one value and print all keys and values.

Summary

You learned that dictionaries store key-value pairs and how to create, read, update, delete, and loop through dictionary data.