Master Python Dictionaries Before Your Next DevOps Interview

 As a DevOps engineer, today I want to explain the second type of data structure in Python: Dictionary — in a very simple way, from my perspective, Raees Yaqoob Qazi.

Understanding Dictionary in Python (Easy Explanation)

What is a Dictionary?

The first question that comes to mind is:

What is a Dictionary?

Let’s understand it with a real-life example.

In the past, we used the Oxford dictionary to find the meaning of a word.
When we search for a word, we get its meaning.

In Python, a dictionary works the same way.

  • The word is called a key
  • The meaning is called a value
  • Together, they form a key–value pair

Basic Example

First, create a file named:

dict_ex.py

Now write this code:

dict_of_items = {
"env": "dev",
"server": "aws linux2",
"ram": 8096,
"cpu": 4,
"active": True
}
print(dict_of_items.get("env"))

Explanation

In this dictionary:

  • "env" is the key
  • "dev" is the value
  • "ram" has value 8096
  • "cpu" has value 4
  • "active" has value True

Just like a real dictionary:

  • env → dev
  • ram → 8096

When we write:

print(dict_of_items.get("env"))

It prints:

dev

Because we searched for the key "env" and got its value "dev".

Task 1

Requirement:

If the environment is active, then print the server details.

Correct Code:

dict_of_items = {
"env": "dev",
"server": "aws linux2",
"ram": 8096,
"cpu": 4,
"active": True
}
if dict_of_items["active"]:
print("Server details")
print("Environment:", dict_of_items["env"])

Output:

Server details
Environment: dev

Explanation

  • We checked if "active" is True
  • If yes, print server details
  • Then print the environment value

Task 2

Now let’s create two environments:

  • Dev → Active = True
  • Stg → Active = False

And we want to print the dictionary which is active.

Code:

dict_of_item_1 = {
"env": "dev",
"server": "aws linux2",
"ram": 8096,
"cpu": 4,
"active": True
}
dict_of_item_2 = {
"env": "stg",
"server": "aws linux2",
"ram": 10240,
"cpu": 8,
"active": False
}
# Add both dictionaries into a list
env_details = [dict_of_item_1, dict_of_item_2]
# Loop through each environment
for env in env_details:
if env["active"] == True:
print("Active Environment Details:")
print(env)

Output:

Active Environment Details:
{'env': 'dev', 'server': 'aws linux2', 'ram': 8096, 'cpu': 4, 'active': True}

Explanation

  1. We created two dictionaries (dev and stg).
  2. We added both into a list.
  3. We used a for loop to iterate.
  4. We checked which dictionary has "active": True.
  5. Only that environment gets printed.

Important Dictionary Functions

If env is your dictionary variable name:

  • env.keys() → Get all keys
  • env.values() → Get all values
  • env.items() → Get both key and value pairs

Example:

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

This prints both key and value together.

Final Words

Dictionary is a very powerful and important data structure in Python.
As DevOps engineers, we use dictionaries everywhere:

  • Handling configuration
  • Managing environment variables
  • Automating infrastructure

I hope this blog makes dictionaries easy to understand from my perspective.

If you found it helpful, please share and follow for more DevOps learning content. 🚀

🌐 Online References


Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator