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