Dictionaries Flashcards

1
Q

What is a dictionary?

A

A set of key value pairs. Keys are unique. They are mutable (can be changed).

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

How can you retrieve the value from a dictionary?
bad_guys = {“batman”: “bane”, “daredevil”: “kingpin”, “x-men”: “apocalypse”}

who is batman’s bad guy?

A

bad_guys[“batman”]

will return:
“bane”

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

what happens if you specify a key that is not in the dictionary?

A

KeyError

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

How do you replace a value in the dictionary?
bad_guys = {“batman”: “bane”, “daredevil”: “kingpin”, “x-men”: “apocalypse”}

replace “apocalpyse” with “juggernaut”

A

bad_guys[“x-men] = “juggernaut”

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

How can you delete a key in a dictionary?

bad_guys = {“batman”: “bane”, “daredevil”: “kingpin”, “x-men”: “apocalypse”}

delete batman

A

del bad_guys[“batman”]

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

True or False - You can access dictionaries with index #s.

A

False. You cannot, you will get KeyError

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

How do you call a specific list value within a dictionary?

amit_linkedIn = {‘fname’:’Amit’, ‘lname’:’Shah’, ‘past_jobs’: [‘gcg’, ‘om’, ‘psc’]}

Return OM

A

amit_linkedIn[‘past_jobs’][1]

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

How do you call a specific dictionary value within a dictionary?

amit_linkedIn = {‘fname’:’Amit’, ‘lname’:’Shah’, ‘past_jobs’: [‘gcg’, ‘om’, ‘psc’], ‘transportation’:{‘car’:’subaru’, ‘subway’:’A’, ‘cab’:[‘uber’, ‘lyft’]}}

Return car

A

amit_linkedIn[‘transportation’][‘car’]

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

True or False - Keys can be used more than once.

A

False

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

True or False - dictionaries cannot be keys

A

True

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

True or False - lists can be keys

A

False

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

How can you check for the existence of keys within a dictionary?

amit_linkedIn = {‘fname’:’Amit’, ‘lname’:’Shah’, ‘past_jobs’: [‘gcg’, ‘om’, ‘psc’]}

Check for past jobs

A

> > > ‘past_jobs’ in amit_linkedIn
True

> > > amit_linkedIn.get(‘past_jobs’)
[‘gcg’, ‘om’, ‘psc’]

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

What does the dictionary.get() function do?

A

It will provide you with the value for the key that is provided in the get function.

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

What does the dictionary.items() function do?

A

It lists out the key value pairs in tuple form.

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

What does the dictionary.keys() function do?

A

Lists out the keys

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

What does the dictionary.values() function do?

A

Lists out the values

17
Q

What does the dictionary.pop(‘key’) function do?

A

removes the ‘key’ from the dictionary, while providing the value removed.

18
Q

What does the dictionary.popitem() function do?

A

removes a random key value pair from the dictionary, while providing the value removed.

19
Q

test = {“a”: “random”, “b”: “rigid”, “c”: “flexible”}

test.update(b=”solid”, c=”flexy”)

notice the = and lack of “ for the keys. What is the new value of test?

A

test = {“a”: “random”, “b”: “solid”, “c”: “flexy”}

20
Q
test = {"a": "random", "b": "rigid", "c": "flexible"}
test2 = {"b"="solid", "c"="flexy"}

test.update(test2)

what is the new value of test?

A

test = {“a”: “random”, “b”=”solid”, “c”=”flexy”}

21
Q
test = {"a": "random", "b": "rigid", "c": "flexible"}
test2 = {"b"="solid", "c"="flexy"}

test.clear()

what is the new value of test?

A

test = {}