Chapter 9 Flashcards
An element in a dictionary has two parts. What are they called?
Key and value
Which part of a dictionary element must be immutable?
The key
Suppose ‘start’ : 1472 is an element in a dictionary. What is the key? What is the value?
The string ‘start’ is the key, and the integer 1472 is the value.
What will the following code display?
stuff = {1 : ‘aaa’, 2 : ‘bbb’, 3 : ‘ccc’}
print(stuff[3])
ccc
Suppose a dictionary named employee has been created. What does the following statement do?
employee[‘id’] = 54321
It stores the key-value pair ‘id’ : 54321 in the employee dictionary.
Suppose a dictionary named inventory exists. What does the following statement do?
del inventory[654]
It deletes the element that has the key 654.
How can you determine whether a key-value pair exists in a dictionary?
You can use the in operator to test for a specific key.
What will the following code display?
stuff = {1 : ‘aaa’, 2 : ‘bbb’, 3 : ‘ccc’}
print(len(stuff))
3
What will the following code display?
stuff = {1 : ‘aaa’, 2 : ‘bbb’, 3 : ‘ccc’}
for k in stuff:
print(k)
1
2
3
What is the difference between the dictionary methods pop and popitem?
The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary.
The popitem method returns a randomly selected key-value pair, as a tuple, and removes that key-value pair from the dictionary.
Assume the following list exists:
names = [‘Chris’, ‘Katie’, ‘Joanne’, ‘Kurt’]
Write a statement that uses a dictionary comprehension to create a dictionary in which each element contains a name from the names list as its key, and the length of that name as its value.
result = {item:len(item) for item in names}
What does the items method return?
It returns all a dictionary’s keys and their associated values as a sequence of tuples.
What does the keys method return?
It returns all the keys in a dictionary as a sequence of tuples.
What does the values method return?
It returns all the values in the dictionary as a sequence of tuples.
Are the elements of a set ordered or unordered?
Unordered
Assume the following dictionary exists:
phonebook = {‘Chris’:’919-555−1111’, ‘Katie’:’828-555−2222’,
’Joanne’:’704-555−3333’, ‘Kurt’:’919-555−3333’}
Write a statement that uses a dictionary comprehension to create a second dictionary containing the elements of phonebook that have a value starting with ‘919’.
phonebook_copy = {k:v for k,v in phonebook.items()
if v.startswith(‘919’)}
After the following statement executes, what elements will be stored in the myset set?
myset = set(‘Jupiter’)
particular order): ‘J’, ‘u’, ‘p’, ‘i’, ‘t’, ‘e’, and ‘r’.
Does a set allow you to store duplicate elements?
No
How do you create an empty set?
You call the built-in set function.
After the following statement executes, what elements will be stored in the myset set?
myset = set(25)
The set will contain one element: 25.
After the following statement executes, what elements will be stored in the myset set?
myset = set(‘www xxx yyy zzz’)
The set will contain these elements (in no particular order): ‘w’, ‘ ‘, ‘x’, ‘y’, and ‘z’.
After the following statement executes, what elements will be stored in the myset set?
myset = set([‘www’, ‘xxx’, ‘yyy’, ‘zzz’])
The set will contain these elements (in no particular order): ‘www’, ‘xxx’, ‘yyy’, and ‘zzz’.
After the following statement executes, what elements will be stored in the myset set?
myset = set([1, 2, 2, 3, 4, 4, 4])
The set will contain these elements (in no particular order): 1, 2, 3, and 4.
How do you determine the number of elements in a set?
You pass the set as an argument to the len function.