Dictionaries Flashcards

1
Q

What types of lists, tuples, and strings can you convert to dicts?

A

dict([‘a’, ‘b’], [‘c’, ‘d’]) = {‘a’:’b’, ‘c’:’d’}
dict((‘a’, ‘b’), (‘c’, ‘d’)) = {‘a’:’b’, ‘c’:’d’}
dict(‘ab’, ‘cd’) = {‘a’:’b’, ‘c’:’d’}

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

How to replace dict key x with ‘y’?

A

dict[x] = ‘y’

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

Can you have {‘a’ : ‘b’, ‘a’ : ‘c’}?

A

No, dict keys need to be unique

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

How do you merge dict x with dict y?

A

x.update(y)

It’s kinda like list.extend(y)

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

How to delete a key:value?

A

del dict[key]

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

How to delete everything from a dict?

A

dict.clear()

it’s a dict method

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

how to see if ‘x’ is in dict?

A

‘x’ in dict

Gives boolean

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

How do you get a key without an error function if it isn’t there?

A

dict.get(key, ‘it’s not there’)

Returns ‘it’s not there’ if key doesn’t exist

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

How to get all keys in a list?

A

dict.keys()

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

How to get all values in a list?

A

dict.values()

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

How to get all items in a list as tuple items in a list?

A

list(dict.items())

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

Difference between set and tuple?

A
set = {x, y}
tuple = (x, y)

Sets don’t have an order and are ridiculously fast, tuples do but are like lists in that you can’t change them directly

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

Things to remember about sets

A
Don't have an order
best time (better than linear)
can't have more than one of a type of thing in them
How well did you know this?
1
Not at all
2
3
4
5
Perfectly