Dictionaries in Python
Md. Shafiuzzaman
Lecturer, Dept. of CSE, JUST
The Dictionary Data Type
• A dictionary is a collection of many values
• Indexes for dictionaries can use many different data types
• Indexes for dictionaries are called keys
• A key with its associated value is called a key-value pair
Dictionaries vs. Lists
• Items in dictionaries are unordered
• Dictionaries can’t be sliced like lists
Example: birthday checker
The keys(), values(), and items() Methods
Covert to list
Assign key and value to separate variables
Checking Whether a Key or Value Exists in a
Dictionary
• Recall what we use in lists
Checking Whether a Key or Value Exists in a
Dictionary
The get() Method
The setdefault() Method
Exercise
• message = “As it is a rainy day, I like to enjoy kichuri and duck vuna”
• Write a program that loops over each character in the message
variable’s string to count how often each character appears
Count how often each character appears in
message
Pretty Printing
• import the pprint module into your programs
Using Data Structures to Model Real-World
Things
• Chessboard
• Tic-Tac-Toe Board
Tic-Tac-Toe Board
Tic-Tac-Toe Board
Tic-Tac-Toe Board
Tic-Tac-Toe Board
Tic-Tac-Toe Board
Tic-Tac-Toe Board
Tic-Tac-Toe Board
Nested Dictionaries and Lists

Dictionaries in Python