Ch.8 Dictionaries
What is a key function?
Tells sorted() to sort based on proxy value
What is a view?
a dynamic (live) object that changes with the dictionary
What should you ALWAYS do before trying to retrieve a key?
a membership test
What is a dictionary?
an unordered collection of key:value pairs
What are two ways to delete an item from a dictionary?
del dictionary_name[key] dictionary_name.pop(key)
How do you set a default returned value when using .pop()?
dictionary_name.pop(key, default value)
What are two ways to look up a corresponding value in a dictionary?
dictionary_name[key] dictionary_name.get(key_name)
How do you add an item to a dictionary?
dictionary_name[key] = value
How do you get an item from a list if the list is a dictionary value?
dictionary_name[key][index]
.keys()
gets all the keys in the dictionary
.items()
gets the current items as a tuple in a dictionary
.values()
gets values from dictionary (returns a view)
Are items in dictionaries mutable or immutable?
immutable
What happens when you assign a value to an existing key
it replaces the old value with the new one
What does .sorted() take as inputs and returns?
iterables list
What is printed when iterating over a dictionary?
its keys
How do you get the highest/lowest values using max/min?
max/min(dictionary_name, key = dictionary_name.get)
How does max/min take an additional key argument?
max/min(dictionary_name, key = proxy)
Is a dictionary itself mutable or immutable?
mutable
Is a set a valid dictionary key? Why?
no keys must be mutable
How many values can each key have?
one
words = {1: 'one', 2: 'two', 3: 'three'} What is words[1]? Why?
one: dictionaries are indexed by key, not by index
How do you print all the dictionary values when iterating over it?
print(key, dictionary_name[key])
.sorted()
returns a sorted list of dictionary keys
How do you sort by dictionary key values, but print the keys?
sorted(dictionary_name, key = dictionary_name.get)
How do you sort by dictionary values?
sorted(dictionary_name.values())
How do you use a key function in .sorted()
sorted(iterable, key = proxy)
How do you get a reverse sort?
sorted(iterable, reverse = True)
What is the purpose of a dictionary?
to store and retrieve values indexed by descriptive keys
Is a list a valid dictionary value?
yes values can be mutable
What happens if you try to delete something that doesn't exist?
you get an error