Dictionaries are a fundamental data structure in Python, allowing you to store key-value pairs for efficient data retrieval. Here's a quick guide:
📝 Key Concepts
- Keys are unique identifiers (e.g., strings, numbers)
- Values can be any data type (e.g., integers, lists, tuples)
- Dictionaries are unordered (as of Python 3.7, insertion order is preserved)
🔧 Basic Operations
# Create a dictionary
my_dict = {"name": "Python", "version": 3.10}
# Access values
print(my_dict["name"]) # Output: Python
# Modify values
my_dict["version"] = 3.11
# Add new key-value pairs
my_dict["features"] = "Dynamic typing"
# Check existence
if "name" in my_dict:
print("Dictionary exists")
📌 Common Methods
Method | Description |
---|---|
keys() |
Returns all keys |
values() |
Returns all values |
items() |
Returns key-value pairs |
get(key) |
Safely retrieves a value |
pop(key) |
Removes a key-value pair |
🧠 Practical Tips
- Use dictionaries for fast lookups (O(1) time complexity)
- Avoid duplicate keys (later values will overwrite earlier ones)
- Combine with loops for advanced data processing
For deeper exploration, check our Python Dictionary Methods tutorial.
Want to learn about nested dictionaries or dictionary comprehensions? Click here to continue your journey!