{Key: Value}

my_dictionary = {
	"entry1": "First person",
	"entry2": "Last person"
}
 

Create new entry:

my_dictionary["entry3"] = "Another person"

Empty the Directory

empty_dictionary = {}

About Iteration

When you run a for loop in dictionaries, it will only give the key values

To list the key-value of dict in a loop:

for question in question_data:
	print(question['text'])

Functions

Dictionary Manipulation

items()

Used to iterate in a dictionary through both Key and Value at the same time

for name, score in student_scores.items(): 
	grade = converter(score) 
	student_grades[name] = grade

max()

Find the biggest number in a list or a dictionary

# List
a = (1, 5, 3, 9)
x = max(a)
 
# Dictionary
scores = {
    "Alice": 50,
    "Zack": 10,
    "Harry" : 88
}
 
x = max(scores, key=scores.get)

This will basically iterate through every “key”, and “.get” function is similar to using ”[]“. So you basically say “Use scores dictionary, and key values are scores[]“

CodeReturnsBased On
max(scores.values())88 (Number)Math
max(scores)”Zack” (String)Alphabet
max(scores, key=scores.get)”Harry” (String)Math

.get()

It can be used to select items in a dictionary like my_dict["Harry"], but won’t break the code if that item is non existent, it will return ‘None’

Link to original