Python Dictionaries
A dictionary in Python is a collection of key-value pairs. Each key is unique, and it maps to a value. Dictionaries are mutable, meaning you can change, add, or remove items after the dictionary has been created. They are unordered, so the items in a dictionary do not maintain any specific order.
Creating a Dictionary
You can create a dictionary by placing key-value pairs inside curly braces {} or by using the dict()
function.
# Creating a dictionary
my_dict = {“name”: “John”, “age”: 25, “city”: “New York”}
print(my_dict) # Output: {‘name’: ‘John’, ‘age’: 25, ‘city’: ‘New York’}
Key | Value |
---|---|
name | John |
age | 25 |
city | New York |
Accessing Dictionary Elements
You can access the value associated with a key by using square brackets [].
my_dict = {“name”: “John”, “age”: 25, “city”: “New York”}
# Accessing value using square brackets
print(my_dict[“name”]) # Output: John
Adding new key with its value in existing dictionary:
You can add new key to existing dictionary by defining key with its value as follows:
my_dict = {“name”: “John”, “age”: 25, “city”: “New York”}
my_dict[‘country’]=’USA’
print(my_dict)
# Output: {“name”: “John”, “age”: 25, “city”: “New York”, “country”:”USA”}
Modifying Dictionary Elements
You can add new key-value pairs or modify existing ones by assigning a value to a key.
# Adding a new key-value pair
my_dict[“city”] = “New Jersey”
print(my_dict) # Output: {‘name’: ‘John’, ‘age’: 25, ‘city’: ‘New Jersey’}
# Modifying an existing value
my_dict[“age”] = 26
print(my_dict) # Output: {‘name’: ‘John’, ‘age’: 26, ‘city’: ‘New York’}
Removing Elements from a Dictionary
You can remove elements from a dictionary using the pop(), del, or popitem() methods.
# Removing an element using pop()
my_dict.pop(“age”)
print(my_dict) # Output: {‘name’: ‘John’, ‘city’: ‘New York’}
# Removing an element using del
del my_dict[“city”]
# Removing the last inserted element using popitem()
my_dict[“age”] = 26
my_dict.popitem()
print(my_dict) # Output: {‘name’: ‘John’}
Multidimensional dictionary
Multidimensional dictionary is basically a dictionary inside a dictionary
address_book = {
‘data1’: {‘name’: ‘John’, ‘age’:10, ‘city’:’New York’},
‘data2’: {‘name’: ‘Jannat’, ‘age’:20, ‘city’:’Mumbai’},
‘data3’: {‘name’: ‘XYZ’, ‘age’:22, ‘city’:’Delhi’},
}
#Accessing data1 in multidimensional dictionary
print(address_book[‘data1’])
#Output: {‘name’: ‘John’, ‘age’: 10, ‘city’: ‘New York’}
#Accessing name from data2
data_2 = address_book[‘data2’]
print(data_2[‘name’])
#output: Jannat
#modifying city in data3
data_3 = address_book[‘data3’]
data_3[‘city’] = ‘Bhopal’
print(data_3)
#Output: {‘name’: ‘XYZ’, ‘age’: 22, ‘city’: ‘Bhopal’}
#now printing entire multidimensional dictionary
print(address_book)
”’
Output:
{‘data1’: {‘name’: ‘John’, ‘age’: 10, ‘city’: ‘New York’},
‘data2’: {‘name’: ‘Jannat’, ‘age’: 20, ‘city’: ‘Mumbai’},
‘data3’: {‘name’: ‘XYZ’, ‘age’: 22, ‘city’: ‘Bhopal’}}
”’
Task:
1. Create a dictionary to store student details like roll no., name, class, division and marks.
Print the dictionary. E.g. student_details = {‘name’:’John’, ‘class’:’VII’, ‘division’:’B’, ‘marks’:50}
2. Given the dictionary student = {‘name’: ‘John’, ‘age’: 21, ‘grade’: ‘A’}, write a program to access and print the value associated with the key “name”.
3. In the dictionary student = {‘name’: ‘John’, ‘age’: 21, ‘grade’: ‘A’}, add a new key “subject” with the value “Math” and update the value of “grade” to “B” and “age” to “22”.
4. Create a phone book using a multidimensional dictionary with ‘phone_number’ as key and value as a dictionary with details of name, age and address. Assume and add data for at least 2 more phone numbers.
e.g. phone_book = {
‘9768876123’ : {‘name’: ‘John’, ‘age’: 42, ’address’: ‘London’}
}