Section 11: Dictionaries Flashcards

1
Q

What are the purpose of dictionaries?

A
  • Keep trach of associations for you
  • Think of a dictionary as an ordered pair:
    1. First element of the pair is referred to as the key
    2. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create a dictionary?

A
  1. Call function dict() (which creates an empty dictionary)
    python
     an_empty_dictionary = dict()
  2. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are keys and values in dictionaries?

A
  1. 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
  2. Values:
    • Values can be of any type
    • Can map to the same value (no uniqueness)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to add an item to a dictionary:

A
  • By specifiying the key and a valuemy_dict[key] = value
  • If key already exists, value is replaced
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to remove an item to a dictionary:

A
  • Delete an item using the following syntaxdel my_dict[key]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Errors

Define KeyError

A

KeyError: raised when try to access/delete an item using a key that does not exist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Useful methods/fuctions for dictionaries?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Checking for membership in a dictionary:

A

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
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Iterating through a dictionary:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Checking for equality

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Compare

Lists vs. Dictionaries

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly