Python: Chapter 9: Dictionaries and Sets
What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} print(len(stuff))
The problem prints the length of the dictionary "stuff" as 3
Suppose a dictionary named inventory exists. What does the following statement do? del inventory[654]
deletes the key-value pair referred by key 654
How to retrieve a value from a dictionary?
dictionary_name[key]
An element in a dictionary has two parts. What are they called?
key-value also referred to as mapping dictionary = {'key' : 'value'}
Add elements to a set
set.add(element)
Removing elements from a set
set.remove(element) or you can use the discard() method
What wil l the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} for k in stuff: print(k)
Output: 1 2 3 It will print the keys
Which part of the dictionary element is immutable?
The key is immutable reasoning: used to map to the value so if you know the key, you can get the value dictionary[key] = value
How to get the number of elements in a set
len(myset)
Creating a set
myset = set() or myset = set(['a', 'b', 'c']) # Can be a list or tuple myset = set('abc') each character would become a separate element in the set
Using the in and not in operator
myset = set([1, 2, 3]) if 1 in myset: print('The value 1 is in the set')
Updating a set
myset = set([1, 2, 3]) myset.update('abc') myset = {'a', 1, 2, 3, 'c', 'b'}
Suppose 'start' : 1472 is an element in a dictionary. What is the key? What is the value?
'start' - key 1472 - value
Sets properties
- no duplicates, only unique values - unordered - can mix data types
Dictionary methods
1. clear() - Clears the contents of a dictionary 2. get() - Gets the value associated with a specified key. if the key is not found, the method does not raise an exception. Instead, it returns a default value. dictionary.get(key, default) default is the default value returned if not found 3. items() - returns all the keys in a dictionary and their associated values as a sequences of tuples 4. keys() - returns all the keys in a dictionary as a sequence of tuples. 5. pop() - Returns the value associated with a specified and removes that key-value pair from the dictionary. If the key is not found, the method a default value. 6. popitem() - returns a randomly selected key-value pair as a tuple from the dictionary and removes that key-value pair from the dictionary 7. values() - returns all the values in the dictionary as a sequence of tuples
What can you do with the dictionary?
1. use in and not in keywords 2. add elements to a dictionary in existing dictionary dictionary_name[key] = value 3. delete elements del dictionary_name[key] 4. Get number of elements from dictionary len(dictionary_name) 5. Mix data types test_scores = {'Kayla' : [88, 92, 100]} 6. Create an empty dictionary dictionary_name = {} 7. use a for loop to iterate over a dictionary for var in dictionary: statement var is a key
Finding subsets
A subset is a set that contains the same but not all elements of another set Ex: set1 = set([1, 2, 3, 4]) set2 = set([2, 3]) Syntax set2.issubset(set1) Alternative syntax set2 <= set1 or set1 >= set2
Finding supersets
A superset contains the subset plus more elements Ex: set1 = set([1, 2, 3, 4]) set2 = set([2, 3]) set1.issuperset(set2) <- returns true Alternative syntax set2 >= set1 or set1 <= set2
What does the keys method return?
It returns all the 'keys' in a dictionary as a sequence of tuples.
What does values method return?
It returns all the 'values' in the dictionary as a sequence of tuples.
Suppose a dictionary named employee has been created. What does the following statement do? employee['id'] = 54321
This statement will add a new key-value pair 'id':54321 in the dictionary employee, IF the key 'id' does not exist If the key 'id' exists, then the statement will replace the value by 54321
How do we create a set containing strings?
You have to use a list like so myset=set(['one', 'two', 'three']) if you don't use a set, it will capture each element as a character myset=set('one', 'two', 'three') elements: 'o', 'n', 'e', etc.
How can you determine if a key-value pair exists in the dictionary?
You use the in operator to determine if the key-value pair exists
Finding the union of the sets
adds all the elements from both sets Syntax set1.union(set2)
Set
an object that stores a collection of data in the same way as mathematical sets
Dictionary
an object that stores a collection of data.
What will the following code display? stuff = {1: 'aaa', 2: 'bbb', 3 : 'ccc'} print(stuff[3]) = ?
ccc
Finding the Difference of Sets
elements that appear in set1 but not in set2 Ex and Syntax: set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.difference(set2) Alternative syntax set3 = set1 - set2
Finding the symmetric difference of sets
elements that are in one set but not in both Okay remember the difference in sets, it shows what elements are in set1 but not in set2, in this case this new set will have the following 1. Elements in set1 that are not in set2 2. Elements in set2 that are not in set1 Example set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.symmetric_difference(set2) set3 = {1, 2, 5, 6} Alternative syntax set3 = set1 ^ set2
Finding the intersection in the set
elements that both sets have in common Syntax set1.intersection(set2) or set1 & set2
Using a for loop to iterate over a set
for var in set: statement var is the name of the variable set is the name of the set