Complete Guide to Dictionaries in Python
What is Dictionaries?
A dictionary is an Ordered(After 3.7) collection of key-value pairs.
Key Characteristics of Dictionaries:
- Ordered: The elements are not stored in any specific order (although, starting from Python 3.7, the insertion
order is preserved).
- Mutable: You can add, update, or remove key-value pairs in a dictionary.
- Key-Value Pairs: A dictionary stores data in pairs, where each key is unique, and each key has a
corresponding value.
- Keys must be immutable: Keys can be strings, numbers, or tuples, but they cannot be lists or other
dictionaries.
- Values can be of any type: Values in a dictionary can be of any data type.
Syntax to define a Dictionary:
dict_name = {key1: value1, key2: value2, key3: value3, ...}
Operations on Dictionaries:
1. Creating a Dictionary:
my_dict = {"name": "Alice", "age": 25, "job": "Engineer"}
print(my_dict) # Output: {'name': 'Alice', 'age': 25, 'job': 'Engineer'}
2. Accessing Values:
print(my_dict["name"]) # Output: Alice
3. Modifying Values:
my_dict["age"] = 26
print(my_dict) # Output: {'name': 'Alice', 'age': 26, 'job': 'Engineer'}
4. Adding Key-Value Pairs:
my_dict["city"] = "New York"
print(my_dict) # Output: {'name': 'Alice', 'age': 26, 'job': 'Engineer', 'city': 'New York'}
5. Removing Key-Value Pairs:
del my_dict["city"]
print(my_dict) # Output: {'name': 'Alice', 'age': 26, 'job': 'Engineer'}
6. Checking if a Key Exists:
if "name" in my_dict:
print("Key exists!")
7. Dictionaries with Nested Structures:
nested_dict = {
"person1": {"name": "John", "age": 30},
"person2": {"name": "Alice", "age": 25}
}
print(nested_dict) # Output: {'person1': {'name': 'John', 'age': 30}, 'person2': {'name': 'Alice', 'age': 25}}
8. Dictionary Methods:
- keys(): Returns all the keys in the dictionary.
- values(): Returns all the values in the dictionary.
- items(): Returns all key-value pairs in the dictionary.
- get(): Retrieves the value for a given key.
- pop(): Removes a key-value pair by key.
print(my_dict.keys()) # Output: dict_keys(['name', 'age', 'job'])
print(my_dict.values()) # Output: dict_values(['Alice', 26, 'Engineer'])
Summary:
- Lists: Ordered, mutable, and can contain elements of various types. You can add, remove, and modify
items.
- Tuples: Ordered, immutable, and can also hold elements of various types. Once created, their content
cannot be changed.
- Dictionaries: Unordered, mutable, key-value pairs where keys are unique and immutable. You can modify,
add, and remove key-value pairs.
Dictionary Programming Questions:
- Write a program to create a dictionary with keys "name", "age", and "job", and corresponding values, and
print the dictionary.
- Write a program to access the value associated with a given key in a dictionary.
- Write a program to add a new key-value pair to an existing dictionary.
- Write a program to remove a key-value pair from a dictionary by key.
- Write a program to check if a given key exists in a dictionary.
- Write a program to merge two dictionaries into one.
- Write a program to find the key with the maximum value in a dictionary.