Section 11: Dictionaries Flashcards
What are the purpose of dictionaries?
- Keep trach of associations for you
- Think of a dictionary as an ordered pair:
- First element of the pair is referred to as the key
- Second elements is referred to as the value
> Dictionary (dict
) said to be mapping type because it maps key object to value objects
- Elements = called items
How to create a dictionary?
- Call function
dict()
(which creates an empty dictionary)python an_empty_dictionary = dict()
- List its elements between curly brackets
python another_empty_dictionary = {} heights = {'Sasha' : 182, 'Rohan' : 168, 'Kate': 177}
Key and value are separated by a colon and items separated by a comma
What are keys
and values
in dictionaries?
- Keys:
- Have to be immutable objects (string, integers, floats, or boolean)
- Have to be unique in a dictionary
Dictionary cannot contain two item with the same key
- Values:
- Values can be of any type
- Can map to the same value (no uniqueness)
How to add an item to a dictionary:
- By specifiying the key and a value
my_dict[key] = value
- If key already exists, value is replaced
How to remove an item to a dictionary:
- Delete an item using the following syntax
del my_dict[key]
Errors
Define KeyError
KeyError: raised when try to access/delete an item using a key that does not exist
Useful methods/fuctions for dictionaries?
-
d.get(key):
Returns the value in the dictionary d associated with the specified key. If the key is not in the dictionary, it returns None. -
d.pop(key):
Removes the key-value pair contained in the dictionary d and returns the value. A KeyError is raised if the key is not in d. -
d.keys():
Returns a list-like object with the keys contained in the dictionary d. -
len(d):
Returns the number of key-value pairs in the dictionary d. -
dict.fromkeys(iterable, value = None):
Creates a new dictionary with keys from iterable and values set to value -
update()
: used to combine two dictionaries
Checking for membership in a dictionary:
Check if key is part of a dictionary using the in operator
```python
my_dict = {‘x’:0, ‘y’:1}
print(‘x’ in my_dict) #prints True
print(0 in my_dict) #prints False
~~~
Iterating through a dictionary:
x maps to 0, y maps to 1, and z maps to 2
Use a for loop
to iteratre through all the keys in a dictionary
```python
my_dict = {‘x’ : 0, ‘y’ : 1, ‘z’ : 2}
for key in my_dict:
print(key, “maps to”, my_dict[key])
~~~
The values
inside a dictionary can be dictionaries themselves.
Can you use dictionaries as keys
No ⇒ keys are immutable and dictionaries are mutable
Checking for equality
- The
==
operator to check if two are equal - Equal if they have the same number of key-value pairs, and each key-value pair in each dictionary is also contained in the order
Compare
Lists vs. Dictionaries
Lists
Useful when you have an ordered list of things, and same element might appear more than once.
Dictionaries
Useful for unordered collections, you would like to ‘index’ on something other than non-negative integers