Python #3: Dictionaries and Key-Value Data

Python dictionaries store information as key-value pairs. They are useful when a program needs to look up a value by a meaningful name instead of only by a numeric position. That makes dictionaries a natural fit for Bitcoin miner telemetry, game-character statistics, sports data, and many everyday applications.

Create a Dictionary

miner = {
    "model": "S21",
    "hashrate_th": 200,
    "efficiency_j_th": 17.5,
    "status": "online"
}

print(miner["model"])
print(miner["hashrate_th"])

Each key identifies a value. Here, "model" points to "S21", while "hashrate_th" points to 200. This resembles the structured telemetry and configuration data used in mining and data-center software.

Read and Update Values

miner["status"] = "maintenance"
miner["temperature_c"] = 68

print(miner["status"])
print(miner["temperature_c"])

Assigning to an existing key updates its value. Assigning to a new key adds another key-value pair.

Use get() for Safer Lookups

fan_speed = miner.get("fan_rpm", "not reported")
print(fan_speed)

Directly requesting a missing key with square brackets raises a KeyError. The get() method can instead return a fallback value.

Gaming Example: Character Stats

player = {
    "name": "Nova",
    "level": 12,
    "health": 95,
    "inventory_slots": 24
}

player["health"] -= 15
print(player)

A game can group related properties for a character under readable keys. Dictionaries are especially useful when the program cares about what each value represents.

Sports Example: Player Records

quarterback = {
    "name": "QB1",
    "passing_yards": 312,
    "touchdowns": 3,
    "interceptions": 0
}

for stat, value in quarterback.items():
    print(stat, value)

The items() method makes it easy to loop through both keys and values. The same pattern can be used for box-score data, game results, entertainment catalogs, or hardware monitoring records.

Nested Dictionaries

mining_farm = {
    "rack_01": {
        "miners": 48,
        "online": 46
    },
    "rack_02": {
        "miners": 48,
        "online": 48
    }
}

print(mining_farm["rack_01"]["online"])

Values inside a dictionary can themselves be dictionaries. Nested dictionaries can represent racks, teams, game worlds, media libraries, or other structured records.

Video Lesson

Programming with Mosh’s Python beginner course includes a dedicated dictionaries chapter beginning around 2:18:21, followed by an emoji-converter exercise that puts dictionary lookup into practice.

Practice

  1. Create a dictionary for one Bitcoin miner with model, hashrate, efficiency, and status.
  2. Add a temperature key after creating the dictionary.
  3. Use get() to request a key that may not exist.
  4. Create either a game-character or sports-player dictionary.
  5. Loop through it with items() and print each key and value.

Key Takeaway

Lists are ideal when position and sequence matter. Dictionaries are ideal when values should be identified by meaningful keys. Real programs frequently combine both structures.

Leave a comment