Computer Science · Ch 10 — Tuples and Dictionaries
Manipulating Dictionaries
Manipulating Dictionaries
Having learnt how to create a dictionary and the methods that operate on it, this section applies those manipulation methods in four complete programs.
Program 1 — a dictionary of odd numbers and its operations
Create a dictionary ODD of odd numbers between 1 and 10, where each key is the decimal number and its value is that number in words, then perform a battery of operations on it:
>>> ODD = {1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven', 9: 'Nine'}
>>> ODD
{1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven', 9: 'Nine'}
(a) Display the keys:
>>> ODD.keys()
dict_keys([1, 3, 5, 7, 9])
(b) Display the values:
>>> ODD.values()
dict_values(['One', 'Three', 'Five', 'Seven', 'Nine'])
(c) Display the items:
>>> ODD.items()
dict_items([(1, 'One'), (3, 'Three'), (5, 'Five'), (7, 'Seven'), (9, 'Nine')])
(d) Find the length of the dictionary:
>>> len(ODD)
5
(e) Check if 7 is present:
>>> 7 in ODD
True
(f) Check if 2 is present:
>>> 2 in ODD
False
(g) Retrieve the value corresponding to the key 9:
>>> ODD.get(9)
'Nine'
(h) Delete the item with key 9:
>>> del ODD[9]
>>> ODD
{1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven'}
Program 2 — employee names and salaries in a dictionary
Names and salaries entered by the user are stored as key-value pairs: the employee's name is the key and the salary is the value. A while loop collects the data; a for loop prints the finished dictionary:
# Program to create a dictionary which stores names of the
# employees and their salaries
num = int(input("Enter the number of employees whose data to be stored: "))
count = 1
employee = dict() # create an empty dictionary
while count <= num:
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary # add one item per employee
count += 1
print("\n\nEMPLOYEE_NAME\tSALARY")
for k in employee:
print(k, '\t\t', employee[k])
Output:
Enter the number of employees whose data to be stored: 3
Enter the name of the Employee: 'Kabir'
Enter the salary: 28000
Enter the name of the Employee: 'Leela'
Enter the salary: 32000
Enter the name of the Employee: 'Manav'
Enter the salary: 26000
EMPLOYEE_NAME SALARY
'Kabir' 28000
'Leela' 32000
'Manav' 26000
Program 3 — count how often each character appears in a string
A dictionary is the natural frequency counter: each distinct character becomes a key and its count is the value. For every character we ask, with the membership operator, whether it is already a key — if yes, increment its count; if it appears for the first time, create the item with count 1:
# Count the number of times a character appears in a given string
st = input("Enter a string: ")
dic = {} # create an empty dictionary
for ch in st:
if ch in dic: # character already seen before
dic[ch] += 1
else:
dic[ch] = 1 # character appears for the first time
for key in dic:
print(key, ':', dic[key])
Output:
Enter a string: HelloWorld
H : 1
e : 1
l : 3
o : 2
W : 1
r : 1
d : 1
Program 4 — convert a number into its number-names
A function converts a number entered by the user into its corresponding words, digit by digit — input 876 should give 'Eight Seven Six'. The digit-to-word mapping lives in a dictionary; the number is kept as a string so that we can walk over its characters:
# Function to convert a number into corresponding number names
def convert(num):
# numberNames maps each digit to its name
numberNames = {0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', \ …