You are on page 1of 16

4.2.

7 Dictionaries

Do Now Questions
• A hash function calculates hash values as follows:
• each character in the key is converted to its ASCII decimal numeric code (i.e. A
will be converted to 65,65)
• the numeric codes are summed to give a total
• the total is divided by the number of slots in the hash table using modular
division
• the result is the hash value
1. Given an alphanumeric Key of “CSAA”, and 55 being the number of
slots, calculate the has value.
2. What is rehashing
3. Given this hashing function below, and a number_of_slots of 48
Calculate the hash values of:
a) 31971
b) 43219
c) 56342
d) ,96756
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Answers

1. 23
2. Over time, items will be added and deleted from the hash table. If
the hash table starts to fill up, or a large number of items are in
the wrong place, the performance of the hash table will degrade.
Rehashing can be used to address this problem.

3.

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Dictionaries

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Objectives
• Know how a dictionary works and how it can be used
• Be aware of simple applications of dictionary in Python

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Dictionaries

• ADT that maps keys to data


• Called an associative array as it deals with two sets of data that are associated
with each other
• Similar to a real life dictionary:
• Word (the key) is associated with a definition (the data) - similar to how a hash table
uses key/value pairs
• Accessed randomly

• Common procedures to carry out on a dictionary – add, retrieve and delete


data

• Data stored is not ordered – different to a real dictionary

• Similar to Hash Tables – dictionaries can be created as hash tables or binary


trees is no data type in language
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Example
• Each record has a key – Customer ID
• Key links all the data that is stored about the customer
• We can retrieve/add/delete a customer record at any point
• Often used to implement databases
• Example:
Key e.g. CustID Associated Data
01234 James Cochran, 12 Harbour Mews, Leicester
If we search for 01235 in
01235 Mary Abbot, 56 Eagle Street, Manchester
the dictionary, Mary
01236 Keith Fletcher, 3 Yarborough Road, Leeds Abbot’s details would be
01237 David Smith, 68 Lemon Street, Derby returned.
01238 Tim Evans, 87 Forrest Drive, Manchester

{01234: “James Conchran, ….”, 01235: “Mary Abbot, ….”, 01236: “Keith Flethcher, …”, ……}

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

What a Dictionary Looks like

Pseudocode Python

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Retrieve a value from a dictionary

Pseudocode Python

Use the key to retrieve associated data. This will return the associated data
value.

The key is used in the same way that an index is used for an array except we use
the key rather than an index.

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Insert a new value

• Key must be unique


• If the key doesn’t exist, it is inserted into the dictionary

Update a value

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Remove a value

Pseudocode Python

If you remove a key, its associated value will be removed as well. If the key
does not exist in the dictionary, an exception will be thrown.

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Dictionaries in Python

• Python has a dictionary data type built in

• Online tutorial using Dictionaries -


https://www.digitalocean.com/community/tutorials/understanding-d
ictionaries-in-python-3

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Python Task

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries

Tasks - Dictionaries

1. Update A3 sheet

Spec Requirements:
1. Be familiar with the concept of a dictionary – collection of
key/value pairs
2. Simple applications of dictionaries e.g. information retrieval
3. Have experience using a dictionary data structure in a
programming language

2. Work through Python tutorial


3. How could you include a dictionary in your project?

Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB

You might also like