Practical Example: Printing Server Inventory Statuses

🏷️ Dictionaries / Iterating Over Dictionaries

🎯 Context Introduction

When managing a fleet of servers, you often need to check their current statusβ€”whether they are online, offline, maintenance, or unknown. A dictionary is a perfect way to store this information because it pairs each server name (the key) with its status (the value). In this example, we will walk through how to build, update, and print a server inventory status report using simple dictionary iteration techniques.


βš™οΈ Building the Server Inventory Dictionary

Start by creating a dictionary that holds the status of several servers. Each server name is a key, and its current status is the value.

  • Server names (keys): "web-server-01", "db-server-02", "cache-node-03", "load-balancer-04"
  • Statuses (values): "online", "offline", "maintenance", "unknown"

Example dictionary structure:

  • web-server-01: online
  • db-server-02: offline
  • cache-node-03: maintenance
  • load-balancer-04: unknown

πŸ› οΈ Iterating Over the Dictionary to Print Statuses

To print each server's status in a clean report format, you loop through the dictionary. There are two common ways to do this:

Method Description Example Output
Loop over keys Use for server in inventory: to access each key, then retrieve the value with inventory[server] web-server-01 is online
Loop over items Use for server, status in inventory.items(): to get both key and value directly db-server-02 is offline

Both methods produce the same result, but the items() approach is often cleaner because it avoids an extra lookup.


πŸ“Š Printing a Formatted Status Report

Once you have the loop in place, you can print a formatted report. For example:

  • Server: web-server-01 β€” Status: online
  • Server: db-server-02 β€” Status: offline
  • Server: cache-node-03 β€” Status: maintenance
  • Server: load-balancer-04 β€” Status: unknown

You can also add a summary at the end, such as:

  • Total servers: 4
  • Online: 1
  • Offline: 1
  • Maintenance: 1
  • Unknown: 1

πŸ•΅οΈ Handling Missing or Unexpected Statuses

In real-world scenarios, a server might have a status that doesn't fit the expected categories. You can add a simple check inside your loop to flag unexpected values.

  • If status is "online", "offline", or "maintenance", print normally.
  • Otherwise, print a warning like "⚠️ Server cache-node-03 has an unknown status: unknown"

This keeps your report accurate and helps you spot issues quickly.


🧩 Putting It All Together

Here is the complete logic flow for printing a server inventory status report:

  1. Create the dictionary with server names and their statuses.
  2. Loop through the dictionary using items() to get each server and status.
  3. Print each server's status in a clear, readable format.
  4. Optionally, count how many servers are in each status category.
  5. Print a summary at the end showing total counts.

Example output of the final report:

  • Server: web-server-01 β€” Status: online
  • Server: db-server-02 β€” Status: offline
  • Server: cache-node-03 β€” Status: maintenance
  • Server: load-balancer-04 β€” Status: unknown
  • --- Summary ---
  • Total servers: 4
  • Online: 1
  • Offline: 1
  • Maintenance: 1
  • Unknown: 1

βœ… Key Takeaways

  • Dictionaries are ideal for storing key-value pairs like server names and statuses.
  • Use for key, value in dictionary.items(): to iterate cleanly over both keys and values.
  • You can format output to make reports easy to read.
  • Adding status checks helps catch unexpected or missing data.
  • A summary section gives a quick overview of the entire inventory.

This practical example gives you a solid foundation for working with dictionaries in real-world scenarios, such as monitoring server fleets, tracking deployment statuses, or managing configuration states.


This section shows how to loop through a dictionary to display server inventory statuses in a readable format.


πŸ”§ Example 1: Basic loop through server names and statuses

This example prints each server name with its current status using a simple for loop.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline"
}

for server_name, status in server_inventory.items():
    print(server_name + " is " + status)

πŸ“€ Output: web-server-01 is online
db-server-01 is online
cache-server-01 is offline


πŸ”§ Example 2: Using keys() to list all server names

This example shows how to iterate only over the server names (keys) in the dictionary.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline"
}

for server_name in server_inventory.keys():
    print("Server name: " + server_name)

πŸ“€ Output: Server name: web-server-01
Server name: db-server-01
Server name: cache-server-01


πŸ”§ Example 3: Using values() to list all statuses

This example demonstrates iterating only over the status values, ignoring server names.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline"
}

for status in server_inventory.values():
    print("Status: " + status)

πŸ“€ Output: Status: online
Status: online
Status: offline


πŸ”§ Example 4: Counting servers by status

This example counts how many servers are online versus offline using a loop.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline",
    "app-server-01": "online",
    "monitor-server-01": "offline"
}

online_count = 0
offline_count = 0

for status in server_inventory.values():
    if status == "online":
        online_count = online_count + 1
    else:
        offline_count = offline_count + 1

print("Online servers: " + str(online_count))
print("Offline servers: " + str(offline_count))

πŸ“€ Output: Online servers: 3
Offline servers: 2


πŸ”§ Example 5: Printing only offline servers for attention

This example filters and prints only servers that need attention (offline status).

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline",
    "app-server-01": "online",
    "monitor-server-01": "offline"
}

print("Servers needing attention:")

for server_name, status in server_inventory.items():
    if status == "offline":
        print("- " + server_name + " is " + status)

πŸ“€ Output: Servers needing attention:
- cache-server-01 is offline
- monitor-server-01 is offline


Comparison Table

Method What it iterates over Use case
.items() Both keys and values When you need server name + status together
.keys() Only keys (server names) When you only need server names
.values() Only values (statuses) When you only need status counts or checks

🎯 Context Introduction

When managing a fleet of servers, you often need to check their current statusβ€”whether they are online, offline, maintenance, or unknown. A dictionary is a perfect way to store this information because it pairs each server name (the key) with its status (the value). In this example, we will walk through how to build, update, and print a server inventory status report using simple dictionary iteration techniques.


βš™οΈ Building the Server Inventory Dictionary

Start by creating a dictionary that holds the status of several servers. Each server name is a key, and its current status is the value.

  • Server names (keys): "web-server-01", "db-server-02", "cache-node-03", "load-balancer-04"
  • Statuses (values): "online", "offline", "maintenance", "unknown"

Example dictionary structure:

  • web-server-01: online
  • db-server-02: offline
  • cache-node-03: maintenance
  • load-balancer-04: unknown

πŸ› οΈ Iterating Over the Dictionary to Print Statuses

To print each server's status in a clean report format, you loop through the dictionary. There are two common ways to do this:

Method Description Example Output
Loop over keys Use for server in inventory: to access each key, then retrieve the value with inventory[server] web-server-01 is online
Loop over items Use for server, status in inventory.items(): to get both key and value directly db-server-02 is offline

Both methods produce the same result, but the items() approach is often cleaner because it avoids an extra lookup.


πŸ“Š Printing a Formatted Status Report

Once you have the loop in place, you can print a formatted report. For example:

  • Server: web-server-01 β€” Status: online
  • Server: db-server-02 β€” Status: offline
  • Server: cache-node-03 β€” Status: maintenance
  • Server: load-balancer-04 β€” Status: unknown

You can also add a summary at the end, such as:

  • Total servers: 4
  • Online: 1
  • Offline: 1
  • Maintenance: 1
  • Unknown: 1

πŸ•΅οΈ Handling Missing or Unexpected Statuses

In real-world scenarios, a server might have a status that doesn't fit the expected categories. You can add a simple check inside your loop to flag unexpected values.

  • If status is "online", "offline", or "maintenance", print normally.
  • Otherwise, print a warning like "⚠️ Server cache-node-03 has an unknown status: unknown"

This keeps your report accurate and helps you spot issues quickly.


🧩 Putting It All Together

Here is the complete logic flow for printing a server inventory status report:

  1. Create the dictionary with server names and their statuses.
  2. Loop through the dictionary using items() to get each server and status.
  3. Print each server's status in a clear, readable format.
  4. Optionally, count how many servers are in each status category.
  5. Print a summary at the end showing total counts.

Example output of the final report:

  • Server: web-server-01 β€” Status: online
  • Server: db-server-02 β€” Status: offline
  • Server: cache-node-03 β€” Status: maintenance
  • Server: load-balancer-04 β€” Status: unknown
  • --- Summary ---
  • Total servers: 4
  • Online: 1
  • Offline: 1
  • Maintenance: 1
  • Unknown: 1

βœ… Key Takeaways

  • Dictionaries are ideal for storing key-value pairs like server names and statuses.
  • Use for key, value in dictionary.items(): to iterate cleanly over both keys and values.
  • You can format output to make reports easy to read.
  • Adding status checks helps catch unexpected or missing data.
  • A summary section gives a quick overview of the entire inventory.

This practical example gives you a solid foundation for working with dictionaries in real-world scenarios, such as monitoring server fleets, tracking deployment statuses, or managing configuration states.

Interactive Views

You are currently in πŸ“š All-in-One mode. Use the tabs at the top to switch to πŸ“– Theory Only or πŸ’» Code Only views.

This section shows how to loop through a dictionary to display server inventory statuses in a readable format.


πŸ”§ Example 1: Basic loop through server names and statuses

This example prints each server name with its current status using a simple for loop.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline"
}

for server_name, status in server_inventory.items():
    print(server_name + " is " + status)

πŸ“€ Output: web-server-01 is online
db-server-01 is online
cache-server-01 is offline


πŸ”§ Example 2: Using keys() to list all server names

This example shows how to iterate only over the server names (keys) in the dictionary.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline"
}

for server_name in server_inventory.keys():
    print("Server name: " + server_name)

πŸ“€ Output: Server name: web-server-01
Server name: db-server-01
Server name: cache-server-01


πŸ”§ Example 3: Using values() to list all statuses

This example demonstrates iterating only over the status values, ignoring server names.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline"
}

for status in server_inventory.values():
    print("Status: " + status)

πŸ“€ Output: Status: online
Status: online
Status: offline


πŸ”§ Example 4: Counting servers by status

This example counts how many servers are online versus offline using a loop.

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline",
    "app-server-01": "online",
    "monitor-server-01": "offline"
}

online_count = 0
offline_count = 0

for status in server_inventory.values():
    if status == "online":
        online_count = online_count + 1
    else:
        offline_count = offline_count + 1

print("Online servers: " + str(online_count))
print("Offline servers: " + str(offline_count))

πŸ“€ Output: Online servers: 3
Offline servers: 2


πŸ”§ Example 5: Printing only offline servers for attention

This example filters and prints only servers that need attention (offline status).

server_inventory = {
    "web-server-01": "online",
    "db-server-01": "online",
    "cache-server-01": "offline",
    "app-server-01": "online",
    "monitor-server-01": "offline"
}

print("Servers needing attention:")

for server_name, status in server_inventory.items():
    if status == "offline":
        print("- " + server_name + " is " + status)

πŸ“€ Output: Servers needing attention:
- cache-server-01 is offline
- monitor-server-01 is offline


Comparison Table

Method What it iterates over Use case
.items() Both keys and values When you need server name + status together
.keys() Only keys (server names) When you only need server names
.values() Only values (statuses) When you only need status counts or checks