Ch 6. Flashcards

1
Q

What is a dictionary in python?

A

A collection of key-value pairs, where each key is connected to a value, and you can use a key to access the value associated with that key.
Note: value can be any of the data types

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

Generic dictionary looks like?

A

dictionary_name = { “a”: “one”, “b” : “two}
where a and b are keys
and one and two are their associated values

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

How do you print the associated value of a key?

A

print(dictionary_name[key])

where the value should be printed

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

How do you add new key-value pairs to a dictionary?

A

dictionary_name[“key”] = “value”

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

When do you typically start off with an empty dictionary?

A

When storing user-supplied data or when you write code that generates a large number of key-value pairs automatically.

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

How do you modify values in a dictionary?

A

Same as adding new key values but of course you are just changing the value

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

How do you remove a key value pair?

A

using the del statement: del dictionary_name[“a”]

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

What is the get() method used for?

A

To set a default value that will be returned if the requested key doesn’t exist.

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

How is get() used?

A

value = foo.get(key)

Value will either be the “value” or None

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

What are the different ways to loop through a dictionary?

A

Can loop through all of the dictionary’s key value pairs, through its keys, or through it’s values.

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

What does the method items() do?

A

Returns a list of key value pairs

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

How do you loop through all key value pairs?

A

for key, value in dictionary_name.items():

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

What is the keys() method useful for?

A

To iterate the keys of the dictionary

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

How do you loop through all the keys in a dictionary?

A

for key_name in dictionary_name.keys()

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

What else can you do with the keys() method?

A

Check if a key is in a dictionary

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

How do you check if a key is in a dictionary?

A

if key_name in dictionary_name.keys()

17
Q

How do you loop though a dictionary’s keys in alphabetical order?

A

for key_name in sorted(dictionary_name.keys()):

18
Q

What is the values() method used for?

A

Returning a list of values w/o any keys.

19
Q

How do you loop through all values in a dictionary?

A

for value_name in dictionary_name.values():

20
Q

What is a set?

A

A collection in which each item must be unique.

21
Q

What happens when you wrap set() around a list that contains duplicate items?

A

Python identifies the unique items in the list and builds a set from those items.

22
Q

A set wraps it’s items in?

A

Braces

23
Q

What is nesting?

A

Storing multiple dictionaries in a list, or a list of items as a value in a dictionary. You can nest dictionaries inside a list, a list of items inside a dictionary, or even a dictionary inside another dictionary.

24
Q

How do you use a list in a dictionary?

A

The value in the dictionary is actually a list. You can nest a list inside a dcitionary any time you want more than one value to be assocaited with a single key in a dictionary.

25
Q

Look up is faster in a list or a set/dict?

A

Set/dict is faster

26
Q

How do you use items()?

A

for key, value in dictionary_name.items()

27
Q

Whats the difference between keys, values and items?

A

.keys() returns keys
.values() returns values
.items() returns a tuple (key, value)

28
Q

Does order matter in a dictionary or set?

A

Nope