Quiz 3- Chapter’s 9-11 Flashcards
(108 cards)
define a dictionary
an object that stores a collection of data.
elements are ordered, changeable and DO NOT allow duplicates
KEY VALUE PAIRS are elements
Is a key immutable or mutable
Key’s are immutable
are dictionaries mutable?
yes
How do you retrieve a specific value from a dictionary?
You use the key associated with it
Dictionaries are ordered. What does that mean
that the items have a defined order and that order will not change. Unordered means that the items do not have a defined order and you cannot refer to an item by using an index
dictionary format:
{key1:val1,key2:val2,key3:val3}
What will this do?
variable = {}
What built-in function will accomplish the same thing?
It will create an empty dictionary.
dict() will accomplish the same thing
what will this do?
cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}
print(cars)
it will display the dictionary.
What will this do?
dictionary[key] = value
Add the new element to the dictionary. If the key already exists it will change the value associated with it
dictionary[key]
What will this do?
retrieve the value from the dictionary. If key is in the dictionary, otherwise KeyError exception is raised.
What’s a way to test and see if a key is in a dictionary?
using the in and not in operators.
if ‘’model’’ in cars:
print(‘’Yes, ‘model’ is a key’’)
cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}
x = cars.values()
print(x)
What is the output?
dict_values([‘Ford’,’Mustang’,1964])
How do you retrieve elements from a dictionary?
cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}
x = cars.items()
print(x)
it will display the key value pair
del dictionary[key]
Will do what?
delete the key-value pair in the dictionary. If key is not in dictionary, KeyError exception is raised.
What does the len function do?
Used to obtain the number of elements in a dictionary.
Key must be immutable but values can be any type of object.
One dictionary can include keys of several different immutable tuples.
Values stored in a single dictionary can be of different types
clear method
deletes all the elements in a dictionary, leaving it empty
format: dictionary.clear()
What does the get method do?
gets a value associated with a specified key from dictionary
format: dictionary.get(key,default)
default is returned if key is not found
alternative to [] operator
CANNOT RAISE KEYERROR EXCEPTION
items method:
returns all the dictionaries keys and values
format: dictionary.items()
returned as dictionary view
each element in dictionary view is a tuple which contains a key and its value
USE A FOR LOOP to iterate over the tuples in the sequence
keys method:
returns all the dictionary keys as a sequence
format: dictionary.keys()
pop method:
returns value with specified key and removes that key-value pair from the dictionary
format: dictionary.pop(key,default)
default is returned if key is not found
popitem method:
returns, as a tuple, the key value pair that was last added to the dictionary. The method also removes the pair from the dictionary.
format: dictionary.popitem()
KEY VALUE PAIR RETURNED AS TUPLE
values method:
returns all the dictionary values as a sequence
format: dictionary.values()
use for loop to iterate over values
dictionary comprehension:
an expression that reads a sequence of input elements and uses those input elements to produce a dictionary
what is this an example of:
squares = {item:item**2 for item in numbers}
dictionary comprehension.
The results expressions is item:item**2
the iteration expression is for item in numbers