Memoization in Recursion
Python Dictionaries
Python dictionaries are unordered collections of key-value pairs. Keys must be immutable (e.g., strings, numbers, tuples), while values can be of any data type.
Key Features:
- Key-Value Pairs: Data is stored as key-value pairs, allowing for efficient lookups using keys.
- Mutability: Dictionaries are mutable, meaning you can add, remove, or modify elements after creation.
- Unordered: Prior to Python 3.7, dictionaries were unordered. While they appear ordered in later versions, relying on this order is not recommended for code portability.
- Hashing: Dictionaries utilize hashing for efficient key lookups. This means that accessing a value by its key is typically very fast, O(1) on average.
Creating Dictionaries:
There are several ways to create dictionaries:
- Using curly braces
{}
:
my_dict = {"name": "Alice", "age": 30, "city": "[New York](./../new-york/)"}
- Using the
dict()
constructor:
my_dict = dict(name="Bob", age=25, city="London")
- From a list of tuples:
my_dict = dict([("name", "Charlie"), ("age", 35), ("city", "Paris")]])
Accessing Elements:
Elements are accessed using their keys:
print(my_dict["name"]]) # Output: Alice (or Bob, or Charlie, depending on which creation method above was used)
If a key doesn’t exist, a KeyError
is raised. Use the get()
method to avoid this:
print(my_dict.get("country", "Unknown")) # Output: Unknown (or the country if it exists)
Adding and Modifying Elements:
my_dict["occupation"]] = "Engineer" #Adding a new key-value pair
my_dict["age"]] = 31 #Modifying an existing key-value pair
Removing Elements:
del my_dict["city"]] # Removes the key-value pair with key "city"
popped_value = my_dict.pop("age") #Removes and returns the value associated with the key "age"
my_dict.popitem() #Removes and returns an arbitrary key-value pair
Iterating through Dictionaries:
#Iterating through keys
for key in my_dict:
print(key)
#Iterating through values
for value in my_dict.values():
print(value)
#Iterating through key-value pairs
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")
Dictionary Methods:
Dictionary Methods (This will be a separate note)
Common Use Cases: Dictionaries are frequently used to represent structured data, such as configurations, data from JSON or other APIs, and more.
Dictionary vs. Lists and Tuples (This will be a separate note)
Example:
person = {
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"zip": "12345"
},
"phone_numbers": ["555-1234", "555-5678"]]
}
print(person["address"]]["city"]]) # Accessing nested dictionaries