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.