XIC CS Dictionary
XIC CS Dictionary
Dictionary in Python is an unordered collection of data, used to store data values along with the keys.
Dictionary holds the key : value pair. We can also refer to a dictionary as a mapping between a set of
keys and a set of values. Each key-value pair in a Dictionary is separated by a colon (:). E.g.
dict={ “a": “alpha", “o": “omega", “g": ”gamma” }
# Creating a Dictionary with Integer Keys Dictionary with the use of Integer
Dict = {1: ‘AAA', 2: ‘BBB', 3: ‘CCC'} Keys:
print("\nDictionary with the use of Integer Keys: ") {1: ‘AAA', 2: ‘BBB', 3: ‘CCC'}
print(Dict)
# Creating a Dictionary with Mixed keys Dictionary with the use of Mixed
Dict = {'Name': ‘Govind', 1: [10, 11, 12, 13]} Keys:
print("\nDictionary with the use of Mixed Keys: ") {'Name': 'Govind', 1: [10, 11, 12, 13]}
print(Dict)
# Creating a Dictionary with dict() method Dictionary with the use of dict():
D=dict({1: 'AAA', 2: 'BBB', 3:'CCC'}) {1: 'AAA', 2: 'BBB', 3: 'CCC'}
print("\nDictionary with the use of dict(): ") Dictionary with each item as a pair:
print(D) {1: 'AAA', 2: 'BBB'}
print("\n all values in the dictionary, one by one:") all values in the dictionary, one by one:
for i in D: AAA BBB CCC
print(D[i], end=' ')
print("\n all keys in the dictionary using keys() all keys in the dictionary using keys()
method:") method:
for i in D.keys(): 123
print(i, end=' ')
print("\n all values in the dictionary using values() all values in the dictionary using
method:") values() method:
for i in D.values(): AAA BBB CCC
print(i, end=' ')
print("\n all keys and values in the dictionary using all keys and values in the dictionary
items()method:") using items()method:
for k, v in D.items(): 1 AAA 2 BBB 3 CCC
print(k, v, end=' ')
64
Built-in functions in dictionary:-
dict() :- dict() function creates a D = dict(name = "Aman", age = 36, country = "India")
dictionary. print(D)
Output:-{'name': 'Aman', 'age': 36, 'country': 'India'}
keys():- returns all the available keys D = dict(name = "Aman", age = 36, country = "India")
print(D.keys())
Output: dict_keys(['name', 'age', 'country'])
values()-returns all the available D = dict(name = "Aman", age = 36, country = "India")
values print(D.values( ))
Output:dict_values(['Aman', 36, 'India'])
65
pop() : method removes the specified D={1:'AAA', 2:'BBB', 3:'CCC'}
item from the dictionary and return print(D.pop(2))
the corresponding value Output : ’BBB’
MIND MAP
MCQ
1. What is the syntax to create an empty dictionary in Python?
a) dict = {}
b) dict = []
c) dict = ()
d) dict = None
Answer: a) dict = {}
66
2. Which method is used to add a new key-value pair to a dictionary?
a) append()
b) update()
c) insert()
d) add()
Answer: b) update()
4. What happens if you try to access a key that doesn't exist in a dictionary?
a) It returns None
b) It raises a KeyError
c) It returns an empty string
d) It returns 0
Answer: b) It raises a KeyError
8. Which method is used to add new key value pair if not exists ?
a) get()
b) setdefault()
c) add()
d) insert()
Answer: a) setdefault()
2. Create a dictionary to store student information, including name, age, and grade. Write a function
to add a new student and another function to retrieve a student's information by name.
3. Given a dictionary of student grades, write a function to calculate the average grade for each
student and return the result in a new dictionary.
Input: {"John": [80, 70, 90], "Jane": [90, 80, 70]}
Output: {"John": 80.0, "Jane": 80.0}
5. Create a dictionary to store book information, including title, author, and price. Write a function to
find the average price of books by a specific author.
6. Given a dictionary of employee data, write a function to find the highest salary and return the
employee's name and salary.
Input: {"John": 50000, "Jane": 60000, "Bob": 70000}
Output: ("Bob", 70000)
7. Write a function to remove duplicate values from a dictionary and return the result.
Input: {"a": 1, "b": 2, "c": 1, "d": 3}
Output: {"a": 1, "b": 2, "d": 3}
8. Create a dictionary to store city information, including name, population, and country.
9. With respect to Q. 8 , Write a function to find cities with a population greater than a given
threshold.
10. Write a small project using the concept learnt so far.
2. Develop a Python program that utilizes a dictionary to store the details of employees in a company,
including their names, ages, and salaries. The program should also calculate the total salary
expenditure.
3. Create a Python dictionary that stores the names of cities as keys and their respective populations
as values. Write a program that includes functions to find the city with the highest population.
4. Design a Python program that employs a dictionary to store the details of books in a library,
including their titles, authors, and publication years. The program should also find books by a specific
author.
5. Write a Python program that utilizes a dictionary to store the details of customers in an e-
commerce platform, including their names, addresses, and order histories. The program should find
the customer with the highest total order value.
6. Develop a Python program that uses a dictionary to store the details of courses in an online
learning platform, including their titles, descriptions, and prices. The program should find courses by
a specific title.
7. Create a Python dictionary that stores the names of countries as keys and their respective capitals
as values. Write a program that finds the capital of a specific country, calculate the number of
countries in the dictionary, and add a new country to the dictionary.
8. Design a Python program that employs a dictionary to store the details of patients in a hospital,
including their names, ages, and medical records. The program should find patients by a specific age
range.
9. Write a Python program that utilizes a dictionary to store the details of products in an inventory
management system, including their names, quantities, and prices. The program should update the
quantity of a product.
10. Develop a Python program that uses a dictionary to store the details of flights in an airline
management system, including their numbers, departure times, and arrival times. The program
should find flights based on its number, and add a new flight to the dictionary.
69