Working with Dictionaries in Python
Dictionaries are a versatile data structure in Python that allow you to store key-value pairs. They are widely used for various purposes, such as storing configuration settings, representing real-world entities, and more. This tutorial will guide you through working with dictionaries in Python.
Creating a Dictionary
In Python, dictionaries are created using curly braces {} and key-value pairs separated by colons :.
Example:
# Create a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict)
Output:
{'name': 'John', 'age': 30, 'city': 'New York'}
Accessing Elements
You can access the value associated with a key in a dictionary using square brackets [].
Example:
# Accessing elements
print(my_dict['name']) # Output: John
print(my_dict['age']) # Output: 30
Modifying Dictionaries
You can modify the value associated with a key or add new key-value pairs to a dictionary.
Example:
# Modifying dictionaries
my_dict['age'] = 35
my_dict['job'] = 'Developer'
print(my_dict)
Output:
{'name': 'John', 'age': 35, 'city': 'New York', 'job': 'Developer'}
Common Dictionary Operations
Python provides several built-in methods for performing common operations on dictionaries, such as getting keys, values, and items.
Example:
# Common dictionary operations
print(my_dict.keys()) # Output: dict_keys(['name', 'age', 'city', 'job'])
print(my_dict.values()) # Output: dict_values(['John', 35, 'New York', 'Developer'])
print(my_dict.items()) # Output: dict_items([('name', 'John'), ('age', 35), ('city', 'New York'), ('job', 'Developer')])
By mastering dictionary operations in Python, you can efficiently manage and manipulate data in your programs.
Related Topics
Related Resources
The Ultimate AI Learning Roadmap for Software Engineers (2025 Edition)
The line between 'software engineer' and 'AI engineer' is disappearing. Are you prepared for the shift from deterministic coding to orchestrating intelligent, probabilistic systems? This comprehensive AI learning roadmap is designed specifically for software professionals. It's a practical, timeline based guide to not only learn the necessary skills but also leverage your existing engineering expertise to transition into a high impact AI role, covering everything from mathematical foundations to production grade MLOps and Generative AI.
articleBuild AI Agents from Scratch with Python and Gemini: A Beginner Friendly Guide to Use Cases and Challenges
AI agents are moving beyond simple chatbots, and with Python and Gemini, beginners can now start building useful autonomous workflows faster than ever. This article introduces AI agents in a practical, beginner friendly way and shows how Python and Gemini can be used to create them from scratch. It covers the core building blocks, a simple development path, real world use cases, and the main challenges to watch for when getting started.
articleList Comprehension in Python
Python is known for its simplicity and readability, and one of the features that truly embodies these qualities is list comprehension. If you’re new to Python, list comprehension might seem a bit intimidating at first, but once you get the hang of it, you’ll see how it can make your code more concise and elegant. In this blog, we’ll explore list comprehension, starting with simple examples and gradually moving on to more complex ones. By the end of this guide, you’ll have a solid understanding of how to use list comprehension in your own projects.