Dictionary Flashcards
1
Q
Dictionaries
A
- Python’s implementation of a data structure, generally known as associative arrays, hashes, or hashmaps.
- Mapping between a set of indexes known as keys and a set of values.
- key:value
2
Q
Create a dictionary to store employee record
A
D = {‘name’: ‘Bob’, ‘age’: 25, ‘job’: ‘Dev’, ‘city’: ‘New York’, ‘email’: ‘bob@web.com’}
3
Q
When do you use Dictionaries vs. list?
A
- Dictionaries items are Unordered and can’t be sorted.
- Dictionaries retieved by key name.
- List are retrieved by location.
- List ordered sequenced can be indexed or sliced.
4
Q
prices_lookup = {‘apple’:2.99, ‘oranges’:1.99, ‘milk’:5.80}
Call the price of apple
milk
oranges
A
prices_lookup[‘apple’]
2.99
prices_lookup[‘milk’]
5.80
prices_lookup[‘oranges’]
1.99
5
Q
- Do dictionaries keep an order?
A
Dictionaries are mappings and do not retain order!
6
Q
Given d={‘k1’:[1,2,3]}
What is the output of d[‘k1’][1]
A
2
7
Q
Is this statement True or False? Dictionaries are immutable.
A
False