Chapter 11 Dictionaries Flashcards
A dictionary contains a collection of indices which are called?
Keys
The association of a key and a value is called a? Or sometimes a?
Key-value-pair or sometimes item
In a dictionary, each key “ “ a value
maps to
Dictionaries are always in alphabetical order. True or False
False
Dictionary elements are in no particular order, unlike lists.
” “ is a method that takes a key and default value. If the key appears in the dictionary, the method returns the corresponding value; otherwise, it returns the default value.
.get
To traverse the keys in alphabetical order, you can use
the built-in function
sorted
Define singleton
A list that contains a single element.
How do I tell Python that I want the variable outside of the function to be used inside the function without alterations?
Use the global statement
»> been_called = False
»> def example2():
global been_called
been_called = True
What is wrong with this code?
»> count = 0
»>def counter():
count = count + 1
print(count)
The global variable was not declared inside the function.
Corrected version:
»> def counter():
global count
count += 1
print(count)
True or False:
If a global variable refers to an immutable value, you can modify the value without declaring the variable.
False. The value must be mutable.
Example:
known = {0:0, 1:1}
def example():
known[2] = 1
4 suggestions for debugging large datasets are:
- Scale down the input
- Check summaries and types
- Write self-checks
- Format the output
Page 111-112 of Think Python
A relationship in which each element of one set corresponds to an element of
another set.
mapping
A mapping from keys to their corresponding values
dictionary
An object that appears in a dictionary as the first part of a key-value pair.
key
In a dictionary, another name for a key-value pair
item
The representation of the mapping from a key to a value
key-value pair
An object that appears in a dictionary as the second part of a key-value pair. This is more specific than our previous use of the word.
value
The algorithm used to implement Python dictionaries.
hashtable
A function used by a hashtable to compute the location for a key.
hash function
A way of performing a computation.
implementation
A type that has a hash function. Immutable types like integers, floats and strings are “______”; mutable types like lists and dictionaries are not.
hashable
A dictionary operation that takes a key and finds the corresponding value.
lookup
A dictionary operation that takes a value and finds one or more keys that map to it.
reverse lookup
A statement that (deliberately) raises an exception
raise statement
A list (or other sequence) with a single element.
singleton
A diagram that shows every frame created during the execution of a program,
with an arrow from each caller to each callee.
Call graph
A computed value stored to avoid unnecessary future computation.
memo
A variable defined outside a function. Can be accessed from any function.
global variable
A boolean variable used to indicate whether a condition is true.
flag
A statement like global that tells the interpreter something about a variable.
declaration