Dictionary Flashcards

1
Q

What is a dictionary?

A

A dictionary is a data structure that is used to stores a collection of items using key, value pairs. Unlike lists which are indexed by position, dictionaries are indexed by keys. Keys must be unique, must be ‘hashable’ type so therefore a list or dictionary cannot be used as a key, hashable values are values that will not change such as numbers and strings.

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

How do we access the values in a dictionary?

A

by using the key in square brackets similar to accessing the index of a list.

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

What is the syntax for adding key/value pairs to an existing dictionary?

A

using the square bracket syntax use the assignment operator to give that key a value.

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

What happens in a dictionary when value is assigned to a key that already exists?

A

The value is overwritten. Each key in a dictionary must be unique.

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

How can we check if a key is present in a dictionary?

A

Simply use the ‘in’ keyword, this will return a Boolean value.

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

What is the syntax for iterating over a dictionary?

A

Use a for-in loop. Note that the value held in the loop variable is a ‘key’.

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

What happens when you try to access a key that does not exist?

A

a ‘keyError’ is thrown

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

How does the update() method work?

A

The update() method allows us to add multiple key value pairs to a dictionary at the same time.

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

How do we delete a key from a dictionary?

A

Use the ‘del’ keyword along with the square bracket syntax and the key you wish to delete.

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

How does the .get() method work with a dictionary?

A

The .get() method will return the value if it exists, otherwise it will return none, or the value used as the optional second argument for the method.

You can also use my_dict.setdefault(‘xx’, ‘key does not exist’) method to get similar behaviour as the .get method.

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

How to remove an item from a dictionary with the .pop() method?

A

Note that using the .pop() method on a dictionary requires at least one argument, i.e. the key to be removed, an optional second argument can be used which will be the value to return if the key does not exist in the dictionary. Remeber that the .pop() method removes the item but also returns the value for use.
EXAMPLE:

raffle = {
223842: “Teddy Bear”,
872921: “Concert Tickets”,
320291: “Gift Basket”,
412123: “Necklace”,
298787: “Pasta Maker”
}

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

What is the result of passing a dictionary as an argument to the list function:
list(my_dict) ?

A

This will return a list of all the keys in the dictionary.
EXAMPLE
test_scores = {
“Grace”: [80, 72, 90],
“Jeffrey”: [88, 68, 81],
“Sylvia”: [80, 82, 84],
“Pedro”: [98, 96, 95],
“Martin”: [78, 80, 78],
“Dina”: [64, 60, 75]
}

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

How does the .keys() method work when called on a dictionary?

A

The .keys() method returns a dict_keys object that contains all the keys in the dictionary, the dict_keys object is a view only object items cannot be removed or added however, It can be iterated.

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

How to get all the values in a dictionary?

A

Use the .values() method. It will return a dict_values object that can be iterated.

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

Using a for-loop and the .items() method, how do we get all the keys and values from a dictionary?

A

Using the .items() method returns a dict_list object which is a tuple in a (key, value) format which can be used in a loop as follows:

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

Syntax for creating a dict comprehension?

A