Dictionary Flashcards
What is a dictionary?
A dictionary is a data structure that is used to stores a collection of items using key, value pairs. Unlike lists which are indexed by position, dictionaries are indexed by keys. Keys must be unique, must be ‘hashable’ type so therefore a list or dictionary cannot be used as a key, hashable values are values that will not change such as numbers and strings.
How do we access the values in a dictionary?
by using the key in square brackets similar to accessing the index of a list.
What is the syntax for adding key/value pairs to an existing dictionary?
using the square bracket syntax use the assignment operator to give that key a value.
What happens in a dictionary when value is assigned to a key that already exists?
The value is overwritten. Each key in a dictionary must be unique.
How can we check if a key is present in a dictionary?
Simply use the ‘in’ keyword, this will return a Boolean value.
What is the syntax for iterating over a dictionary?
Use a for-in loop. Note that the value held in the loop variable is a ‘key’.
What happens when you try to access a key that does not exist?
a ‘keyError’ is thrown
How does the update() method work?
The update() method allows us to add multiple key value pairs to a dictionary at the same time.
How do we delete a key from a dictionary?
Use the ‘del’ keyword along with the square bracket syntax and the key you wish to delete.
How does the .get() method work with a dictionary?
The .get() method will return the value if it exists, otherwise it will return none, or the value used as the optional second argument for the method.
You can also use my_dict.setdefault(‘xx’, ‘key does not exist’) method to get similar behaviour as the .get method.
How to remove an item from a dictionary with the .pop() method?
Note that using the .pop() method on a dictionary requires at least one argument, i.e. the key to be removed, an optional second argument can be used which will be the value to return if the key does not exist in the dictionary. Remeber that the .pop() method removes the item but also returns the value for use.
EXAMPLE:
raffle = {
223842: “Teddy Bear”,
872921: “Concert Tickets”,
320291: “Gift Basket”,
412123: “Necklace”,
298787: “Pasta Maker”
}
What is the result of passing a dictionary as an argument to the list function:
list(my_dict) ?
This will return a list of all the keys in the dictionary.
EXAMPLE
test_scores = {
“Grace”: [80, 72, 90],
“Jeffrey”: [88, 68, 81],
“Sylvia”: [80, 82, 84],
“Pedro”: [98, 96, 95],
“Martin”: [78, 80, 78],
“Dina”: [64, 60, 75]
}
How does the .keys() method work when called on a dictionary?
The .keys() method returns a dict_keys object that contains all the keys in the dictionary, the dict_keys object is a view only object items cannot be removed or added however, It can be iterated.
How to get all the values in a dictionary?
Use the .values() method. It will return a dict_values object that can be iterated.
Using a for-loop and the .items() method, how do we get all the keys and values from a dictionary?
Using the .items() method returns a dict_list object which is a tuple in a (key, value) format which can be used in a loop as follows: