Dictionary Flashcards

1
Q

What is a dict (dictionary)?

A

in Python it is a data type and a data structure

an unordered collection of key: value pairs

consists of key: value pairs

a key: a string for us to grab the value. It is the key to access the value

a value:

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

Dictionary keys

A

a dictionary key must be immutable, cannot change

therefore we cannot use a list as a key since a list can be modified

a key should be descriptive

a key must be unique as it can only exist in the dictionary once. If you try to use the same key twice, the previous key: value pair will be lost

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

Dictionary methods

A

.get: takes in a key as an argument and returns None if the key does not exist. If it does exist it will return the value associated with the key

dict(key=value) is a built-in function that allows you to create a dictionary. Although, this is not commonly used

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