Chapter 9 Dictionaries and Sets Flashcards
You can use the ___ operator to determine whether a key exists in a dictionary.
in
The in operator determine whether a value exists in a set.
You use ___ to delete an element from a dictionary.
the del method
You can delete an existing key-value pair from a dictionary with the del statement:
del dictionary_name[key]
The ___ function returns the number of elements in a dictionary:
len( )
You can use the built-in len function to get the number of elements in a dictionary.
You can use ___ to create an empty dictionary.
{ }
The ___ method returns a randomly selected key-value pair from a dictionary.
popitem
The popitem method returns a randomly selected key-value pair, and it removes the key-value pair from the dictionary.
The ___ method returns the value associated with a specified key and removes that key_value pair from the dictionary.
pop()
The pop method returns the value associated with a specified key and removes that key-value pair from the dictionary.
if the key is not found, the method returns a default value.
dictionary.pop(key, default)
The ___ dictionary method returns the value associated with a specific key. If the key is not found, it returns a default value.
get()
The get method as an alternative to the [] operator for getting a value from a dictionary. The get method does not raise an exception if the specified key is not found.
The following function returns the number of elements in a set:
len()
You can add one element to a set with this method
add()
You use the add method to add n element to a set.
You can add a group of elements to a set with this method.
update
You can add a group of elements to a set all at one time with the update method.
Sets are mutable objects so you can add items and remove items from them.
The set method removes an element, but does not raise an exception if the element is not found.
discard
This set method removes an element and raises an exception if the element is not found.
remove
This operator can be used to find the union of two sets.
|
The union of two set that contains all the elements of both sets.
set1.union(set2) or set1 | set 2
This operator can be used to find the difference of two sets.
-
The difference method get the difference of two sets. The difference of set 1 and set 2 is the elements that appear in set 1 but not appear in set 2.
set1 - set2
This operator can be used to find the symmetric difference of two sets.
The symmetric difference of two sets is a set that contains the elements that are not shared by the sets
The ___ method returns all of a dictionary’s keys and their associated values as a sequence of tuples.
items()
The items method returns all of a dictionary’s key and their associated values.
Which method would you use to get all the elements in a dictionary returned as a list of tuples?
items
What will be the result of the following code?
ages = {‘Aaron’ : 6, ‘Kelly’ : 3, ‘Abigail’ : 1}
value = ages{‘Brianna’}
KeyError
A dictionary can include the same value several times but cannot include the same key several times.
True or False?
True
Sets are created using curly braces {}
True or False?
False
The set remove and discard methods behave differently only when a specified item is not found in the set.
True or False?
True
What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?
{1 : ‘January’, 2 : ‘February’, …. 12 : ‘December’}
What is the value of the variable phones after the following code executes?
phone = {‘John’ : ‘5555555’, ‘Julie’ : ‘5557777’, }
phone {‘John’} = ‘5556666’
{‘John’ : ‘5556666’, ‘Julie’ : ‘5557777’, }
Which method would you use to get all the elements in a dictionary returned a s a list of tuples?
a. pop
b. list
c. items
d. keys
items
Chapter 9 Q (page# 448)
The items method returns all of a dictionary’s keys and their associated values. They are returned as a special type of sequence known as a dictionary view. Each element in the dictionary view is a tuple, and each tuple contains a key and its associated value.
A dictionary can include the same value several times but cannot include the same key several times.
True or False?
True
Chapter 9 Q
What does the get method do if the specified key is not found in the dictionary?
a. It throws an exception.
b. It does nothing.
c. it returns a default value.
d. You cannot use the get method to specify a key.
It returns a default value.
Chapter 9 Q
What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order).
cities = {'GA' : 'Atlanta', 'NY' ; 'Albany', 'CA' : 'San Diego'} if 'CA' in cities: del cities['CA'] cities['CA'] = 'Sacramento' print(cities)
a. {‘CA’ : ‘Sacramento’}
b. {‘CA’ : ‘Sacramento’, ‘NY’ : ‘Albany’, ‘GA’ : ‘Atlanta’}
c. {‘CA’ : ‘Sacramento’}
d. {‘NY’ : ‘Albany’, ‘GA’ : ‘Atlanta’}
{‘CA’ : ‘Sacramento’, ‘NY’ : ‘Albany’, ‘GA’ : ‘Atlanta’}
Chapter 9 Q
The issubset() method can be used to determine whether set1 is a subset of set2? True or False?
True
Chapter 9 Q
If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
True or False?
True
Chapter 9 Q
The difference of set1 and set2 is a set that contains only the elements that appear in set 1 but do not appear in set2.
True or False
True
Chapter 9 Q
>> set1 = set([1, 2, 3, 4]) >> set2 = set([3, 4, 5, 6]) >> set3 = set1 - set2 >> set3 {1, 2}
The elements in a dictionary are store in ascending order, by the keys of the key-value pairs.
True or False?
False
Chapter 9 Q
The union of two sets is a set that contains only the elements that appear in both sets.
True or False?
False
Chapter 9 Q (page# 467)
The union of two sets is a set that contains all the elements of both sets