A Python Dictionary is an unordered list where items have matching information.
Let's say we had a list of students and we wanted to make notes about their favourite color
Bob - red
Susie - blue
Chuck - green
We can store this in a Dictionary!
favcolors = { "Bob" : "red", "Susie" : "blue", "Chuck" : "green"}
Note some things about this dictionary:
Another way of looking at them is this:
Using Items From A Dictionary
Dictionaries do not have indexes like lists.
Instead, you can search by a key and you get its pair.
The keys for our example are : Bob, Susie, Chuck
The key-pair value for "Bob" is "red".
The key-pair value for "Susie" is "blue".
To access a specific key-pair, use the format:
dictionaryname [key]
keys can be any type of immutable data/variable - strings, numbers or tuples
key-pairs can be any type of data - strings, numbers, tuples, lists
Try Example 1:
Line 2 prints the entire dictionary
Line 3 to 5 prints the matching pair to whatever name is in [square brackets]
How to change or add dictionary elements
Since Dictionaries do not have indexes, they're easier to change and add to.
To add an item follow this format:
dictionaryname[newkey] = newkeypair
To change an item follow this format:
dictionaryname[key] = newkeypair
Try this example where we add a new person with their favourite color and then change Bob's favourite color to purple:
How to delete or remove dictionary elements
To delete an item in a dictionary use this format:
del dictionaryname[key]
You can also use Pop the same way we did in lists. Pop turns the code where you used Pop into the key you're deleting. Use this format with round brackets:
dictionaryname.pop(key)
Try this example where we delete and pop the favcolors dictionary (you can modify the one from before):
Built-In Dictionary Functions (with links to further references)
Return True
if all keys of the dictionary are true (or if the dictionary is empty).
Return True
if any key of the dictionary is true. If the dictionary is empty, return False.
Return the length (the number of items) in the dictionary.
cmp()
Compares items of two dictionaries.
Return a new sorted list of keys in the dictionary.
Python Dictionary Methods
Remove all items form the dictionary.
Return a shallow copy of the dictionary.
Return a new dictionary with keys from seq and value equal to v(defaults to None
).
Return the value of key. If key doesnot exit, return d (defaults to None
).
Return a new view of the dictionary's items (key, value).
Return a new view of the dictionary's keys.
Remove the item with key and return its value or d if key is not found. If d is not provided and key is not found, raises KeyError
.
Remove and return an arbitary item (key, value). Raises KeyError
if the dictionary is empty.
If key is in the dictionary, return its value. If not, insert key with a value of d and return d (defaults to None
).
Update the dictionary with the key/value pairs from other, overwriting existing keys.
Return a new view of the dictionary's values
Do the Lesson 12 Repl assignments! There are 2 to enjoy right now!