Remember
A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.
A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
scores = {"Ada": 3}
scores["Ada"] = scores["Ada"] + 1
scores["Ben"] = 5
print(scores)Top-level scope
{'Ada': 4, 'Ben': 5}A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.
(no output)
What do you expect this program to print?
DICTIONARY +-------------+-------------+ | KEY | VALUE | +-------------+-------------+ | "name" | "Ada" | | "language" | "Python" | +-------------+-------------+ record["name"] --> "Ada"
Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
scores = {"Ada": 3}
scores["Ada"] = scores["Ada"] + 1
scores["Ben"] = 5
print(scores)Top-level scope
{'Ada': 4, 'Ben': 5}The example has finished. Compare the final output with the values and references above.
{'Ada': 4, 'Ben': 5}
What do you expect this program to print?
scores = {"Ada": 3}
scores["Ada"] = scores["Ada"] + 1
scores["Ben"] = 5
print(scores)A Python dictionary is an unordered collection that stores data as key-value pairs. Each key is unique, and each key maps to a value. You can use the key to access the associated value. Think of it as a real-world dictionary where you look up a word (key) and get its meaning (value).
my_dict = {
"name": "Arjun",
"age": 25,
"city": "New York"
}
In this dictionary, the keys are "name", "age", and "city". The corresponding values are "Arjun", 25, and "New York".
Dictionaries are very useful when you need to store data that is associated with a unique identifier (the key). For example, storing student names with their grades, a person's contact information (name, phone number, etc.), or the countries and their capitals.
To create a dictionary, you can use curly braces {} and separate keys and values with a colon :.
student = {
"name": "Arjun",
"age": 22,
"grade": "A"
}
To get the value associated with a key, you can use square brackets [] with the key.
student = {
"name": "Arjun",
"age": 22,
"grade": "A"
}
print(student["name"])
Arjun
If you want to change the value associated with a key, you can directly assign a new value to that key.
student = {
"name": "Arjun",
"age": 22,
"grade": "A"
}
student["age"] = 23
print(student)
{'name': 'Arjun', 'age': 23, 'grade': 'A'}
You can add a new key-value pair to the dictionary by assigning a value to a new key.
student = {
"name": "Arjun",
"age": 22,
"grade": "A"
}
student["city"] = "London"
print(student)
{'name': 'Arjun', 'age': 22, 'grade': 'A', 'city': 'London'}
If you want to remove a key-value pair from the dictionary, you can use the del keyword.
student = {
"name": "Arjun",
"age": 22,
"grade": "A"
}
del student["grade"]
print(student)
{'name': 'Arjun', 'age': 22}
my_dict = {} to create a dictionary.my_dict[key] to access a value by key.my_dict[key] = new_value to update a value.my_dict[new_key] = new_value to add a new key-value pair.del my_dict[key] to delete a key-value pair.Connect what you noticed in the example with the idea below.
A dictionary associates unique keys with values. Assigning an existing key replaces its value, while a new key adds an entry.
Assigning the same key again adds a duplicate key.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.