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.pyNow 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 value8096"cpu"has value4"active"has valueTrue
Just like a real dictionary:
- env → dev
- ram → 8096
When we write:
print(dict_of_items.get("env"))It prints:
devBecause 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: devExplanation
- We checked if
"active"isTrue - 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
- We created two dictionaries (dev and stg).
- We added both into a list.
- We used a
forloop to iterate. - We checked which dictionary has
"active": True. - Only that environment gets printed.
Important Dictionary Functions
If env is your dictionary variable name:
env.keys()→ Get all keysenv.values()→ Get all valuesenv.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
- LinkedIn: https://www.linkedin.com/in/raees-yaqoob-qazi-ryqs/
- Medium Blog: https://medium.com/@raeesyaqubqazi
- Blogspot: https://brillertechnologies.blogspot.com/
- Facebook Page: https://web.facebook.com/profile.php?id=61553548371216
- YouTube Channel: https://www.youtube.com/@RaeesQ.
- TikTok: https://www.tiktok.com/@mrryqs?_t=ZS-8y7t0fQfJKu&_r=1
Comments
Post a Comment