You are on page 1of 11

Computer Programming

ENEE 101

Lec#18 (Sequences part 4 – List conclusions and Dictionaries)


Department of Electrical and Electronic Engineering
University of Jeddah 1
Dictionaries

• The data structure is a called a An example is a dictionary of students


“dictionary” because it which contains ID/names pairs.

resembles a word dictionary,


where the words are the keys
and the words definitions are
the values.

• A dictionary is also known as a


map, which maps each key to a
value.
2
Create a Dictionary
• You can create a dictionary by enclosing the items inside a pair of
curly braces ( {} ).

• myDictionary= {}
 It creates an empty dictionary

• students = {"111-34-3434":"John", "132-56-6290":"Peter"}

 It creates a dictionary with two items, as shown in the previous


Figure.

 The key of the first item is “111-34-3434” , and its


corresponding value is John.

3
Dictionary syntax

• students = {"111-34-3434":"John", "132-56-6290":"Peter"}

 Each item consists of a key-value pair separated by a colon.

 The items are separated by commas.

 The key must be of a hashable type such as numbers and


strings.

 In this case we used a string type for the keys to allow leading
zeros.

 The value can be of any type.

4
Dictionary usage 1/4
• To add an item to a dictionary, use the syntax: dictionaryName[key] = value

• For example: students[“234-56-9010”] = “Susan”

• If the key is already in the dictionary, the preceding statement replaces the
value for the key (update the value corresponding the value).

• To retrieve a value, simply write an expression using: dictionaryName[key]

• If the key is in the dictionary, the value for the key is returned. Otherwise, a
KeyError exception is raised.
5
Dictionary usage 2/4

• To delete an item from a dictionary, use the syntax:del dictionaryName[key]

• For example: del students[“234-56-9010”]


 This statement deletes, from the dictionary, the item with the key
“234-56-9010”.

• If the key is not in the dictionary, a KeyError exception is raised.


6
Dictionary usage 3/4
You can use a for loop to traverse all keys in the dictionary. For example:

The for loop iterates on the keys in the dictionary students (line 2).
students[key] returns the value for the key key (line 3).

You can find the number of the items in a dictionary by using len(dictionary).
For example:

7
Dictionary usage 4/4
You can use the in or not in operator to determine whether a key is in the
dictionary. For example:

You can use the == and != operators to test whether two dictionaries contain
the same items. For example:

No issues regarding the


order, dictionaries are
not ordered.

You cannot use the comparison operators ( > , >= , <= , and < ) to compare
dictionaries because the items are not ordered. 8
Dictionary methods

• The get(key) method is similar to dictionaryName[key] except that the


get method returns None if the key is not in the dictionary rather than
raising an exception.
• The pop(key) method is the same as del dictionaryName[key] but it
returns the value of the deleted pair.

9
Dictionary methods examples

10
Dictionary recap

Examples for a students’ dictionary:


Students={id1:[name1, phone1, mark1_1, mark1_2, ...], id1:[name1, phone1, mark1_1, mark1_2, ...], …}
Students={200000:[“Ali”, “0599000000”, 60, 61, 62], 200001:[“Jawal”, “0599000001”, 59, 60, 61]}
Students[200000] will give the student record for the provided id: [“Ali”, “0599000000”, 60, 61, 62]
Students[200000][0] will give the student name for the same id: “Ali”
11

You might also like