Dictionary Question - Practice Questions
Dictionary Question - Practice Questions
Given a dictionary student_scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78}, write a
Python code to:
a) Add a new student named 'David' with a score of 88.
b) Update Alice's score to 90.
c) Remove Charlie from the dictionary.
d) Print the final dictionary.
2. Write a Python function that takes a list of words and returns a dictionary with each
word as a key and its frequency count as the value.
• Input: words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
• Output: {'apple': 3, 'banana': 2, 'orange': 1}
3. You have two dictionaries representing inventory levels for two different warehouses:
• warehouse1 = {'apples': 30, 'bananas': 15}
• warehouse2 = {'oranges': 25, 'apples': 20}
Write a Python code to merge these dictionaries so that the quantities of items
with the same key are summed. The result should be:
• Output: {'apples': 50, 'bananas': 15, 'oranges': 25}
4. Write a Python function that filters a dictionary by keeping only the entries where the
values are above a certain threshold.
• Input: scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78, 'David': 88}, threshold = 80
• Output: {'Alice': 85, 'Bob': 92, 'David': 88}
5. Write a Python function that takes two strings and returns True if they are anagrams of
each other and False otherwise. Use a dictionary to count character occurrences in
each string.
• Input: s1 = 'listen', s2 = 'silent'
• Output: True
6. Given a list of tuples where each tuple represents a student and their grade, write a
Python function that groups the students by their grades using a dictionary.
• Input: students = [('Alice', 'A'), ('Bob', 'B'), ('Charlie', 'A'), ('David', 'C')]
• Output: {'A': ['Alice', 'Charlie'], 'B': ['Bob'], 'C': ['David']}
7. Write a Python function to count the number of occurrences of each letter in a given
string using collections.defaultdict.
• Input: s = 'programming'
• Output: {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}
8. Create a dictionary comprehension that generates a dictionary from a list of numbers
where the keys are the numbers and the values are the squares of those numbers.
• Input: numbers = [1, 2, 3, 4, 5]
• Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Inventory
You are managing an inventory for a small store. The store has different items, and each item
has a unique ID, a name, and a quantity in stock.
Question: Write a Python dictionary to represent the inventory. Then, write a code snippet to:
1. Add a new item to the inventory.
2. Update the quantity of an existing item.
3. Remove an item that is no longer available.
4. Find the total number of items in the inventory.
Student Grades
You have a list of students and their grades for a particular subject. You need to perform
various operations to manage and analyze the grades.
Question:
1. Create a dictionary where the keys are student names and the values are their grades.
2. Find the grade of a specific student.
3. Update the grade of a student.
4. Find the average grade of the class.
Word Frequency Counter
You are given a text document, and you need to count the frequency of each word in the
document.
Question:
1. Write a Python program to read the text and store the word frequency in a dictionary.
2. Identify the most common word(s) in the document.
3. Remove common stop words like "the", "and", "is" from the word frequency
dictionary.
9.
Question: Create a dictionary named student_grades with three students and their
corresponding grades: John (85), Alice (92), and Bob (78). Then, add a new student, Emma,
with a grade of 88. Print the updated dictionary.
o Solution:
student_grades = {'John': 85, 'Alice': 92, 'Bob': 78}
student_grades['Emma'] = 88
print(student_grades)
2. Accessing and Modifying Dictionary Values:
• Question: Given a dictionary inventory = {'apples': 10, 'oranges': 5, 'bananas': 8},
write a Python program to increase the count of apples by 5 and decrease the count of
oranges by 2. Print the modified dictionary.
o Solution:
inventory = {'apples': 10, 'oranges': 5, 'bananas': 8}
inventory['apples'] += 5
inventory['oranges'] -= 2
print(inventory)
3. Deleting Items from a Dictionary:
• Question: Create a dictionary employees with the following key-value pairs: {'John':
'Manager', 'Alice': 'Developer', 'Bob': 'Analyst'}. Remove 'Alice' from the dictionary
and print the updated dictionary.
o Solution:
python
Copy code
employees = {'John': 'Manager', 'Alice': 'Developer', 'Bob': 'Analyst'}
del employees['Alice']
print(employees)
4. Check if a Key Exists in a Dictionary:
• Question: Write a Python function check_key(dictionary, key) that takes a dictionary
and a key as input and returns True if the key exists in the dictionary, otherwise False.
Test it with {'a': 1, 'b': 2, 'c': 3} and the key 'b'.
o Solution:
python
Copy code
def check_key(dictionary, key):
return key in dictionary