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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a dictionary to store employee record

A

D = {‘name’: ‘Bob’, ‘age’: 25, ‘job’: ‘Dev’, ‘city’: ‘New York’, ‘email’: ‘bob@web.com’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  • Do dictionaries keep an order?
A

Dictionaries are mappings and do not retain order!

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

Given d={‘k1’:[1,2,3]}

What is the output of d[‘k1’][1]

A

2

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

Is this statement True or False? Dictionaries are immutable.

A

False

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