1. Home
2. Docs
3. Exercises
4. Dictionary Exercises

# Dictionary Exercises

In this section Dictionary Exercises are given. Learn about dictionaries here.

Try these on your own then check out the solutions.

Exercise 1: create a employee and dept database.

Get employee details by employee id.

Ex: dept-data

Dept code Name
—————–
101 ‘HRD’,
102 ‘FINANCE’,
103 ‘ACCOUNTS’,
104 ‘SALES’,
105 ‘ENGINEERING’,
106 ‘SUPPORT’

Employee data sample

name doj emp id

‘Alex’ 10-10-17 1000

Solution:

```
#create employee database

#create dept database

#map employee to a dept

#Provide option to retrieve the info by emp_id , emp_name

dept_db ={
101 : 'HRD',
102 : 'FINANCE',
103 : 'ACCOUNTS',
104 : 'SALES',
105 : 'ENGINEERING',
106 : 'SUPPORT'
}

employee_db = {
1000: dict(name="Alex",   doj='01-10-89', dept=103),
1001: dict(name="Mary",   doj='01-11-88', dept=101),
1002: dict(name="John",   doj='01-10-87', dept=104),
1003 : dict(name="David",  doj='01-06-89', dept=105),
1004 : dict(name="Anne",   doj='01-01-86', dept=106),
1005 : dict(name="Samson", doj='01-02-89', dept=101)
}

#print(dept_db)

print("#####################################")

#print(employee_db)

def get_emp_info(emp_id):

if emp_id in employee_db:

return employee_db[emp_id]

else:

print(emp_id, " doesn't exists ")

return

def display_emp_data(emp_data):

for k,v in emp_data.items():

print(k, " : ",v)

emp_id = int(input("Please Enter Emp Id :"))

emp_data = get_emp_info(emp_id)

if emp_data:

#print(emp_data)

display_emp_data(emp_data)

```

Exercise 2: Display products with price less or equal form user input

products = {
‘SMART WATCH’: 550,
‘PHONE’ : 1000,
‘PLAYSTATION’: 500,
‘LAPTOP’ : 1550,
‘MUSIC PLAYER’ : 600,
‘TABLET’ : 400

}

Solution:

```products = {
'SMART WATCH': 550,
'PHONE' : 1000,
'PLAYSTATION': 500,
'LAPTOP' : 1550,
'MUSIC PLAYER' : 600,
'TABLET' : 400

}

input_price = int(input("Enter the price :"))

def display_product(given_db,price):

for item in products:

if products[item] <= input_price:

print(item, " : ",products[item])

display_product(products, input_price)

```

Exercise 3: Count number of occurrence of words in a given text.

Sample input:

This is a common message for people,

message is private and circulate this with caution

Solution:

```def word_conter(text_msg):

num_words = {}

for word in text_msg.split():

word = word.lower().strip()

num_words[word] = num_words.get(word,0) + 1

return num_words

message = '''This is a common message for people,

message is private and circulate this with caution '''

word_conter(message)

output:

defaultdict(int,
{'a': 1,
'and': 1,
'caution': 1,
'circulate': 1,
'common': 1,
'for': 1,
'is': 2,
'message': 2,
'people,': 1,
'private': 1,
'this': 2,
'with': 1})

```

Using collection module defaultdict() factory function.

```from collections import defaultdict

def word_conter(text_msg):

num_words = defaultdict(int)

for word in text_msg.split():

word = word.lower().strip()

num_words[word] += 1

return num_words

message = '''This is a common message for people,

message is private and circulate this with caution '''

word_conter(message)

output:

defaultdict(int,
{'a': 1,
'and': 1,
'caution': 1,
'circulate': 1,
'common': 1,
'for': 1,
'is': 2,
'message': 2,
'people,': 1,
'private': 1,
'this': 2,
'with': 1})

```