Working with Dictionaries and Sets Flashcards

1
Q

Question: Consider a dictionary of names and ages set up as below:

Code Editor:
names_ages = {‘John’: 35, ‘Jim’: 45, ‘Alice’: 25}

How would you look up Alice’s age in this dictionary?

A

names_ages[‘Alice’]

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

A set in Python can contain which of the following data types?

Floats
Strings
Tuples
Dictionaries
Lists

A

A set in python contains
Floats
Strings
Tuples

it cannot contain dictionary, Lists

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

Question: Consider a nested list of names and ages:

Code Editor:
names_ages = [[‘John’, 35], [‘Jill’, 38], [‘Tim’, 27]]

How would you convert this to a dictionary with names as the keys and ages as values?

A

dict(names_ages)

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

Question: Consider a nested list of names and ages:

Code Editor:
names_ages = [[‘John’, 35], [‘Jill’, 38], [‘Tim’, 27]]

How would you access Tim’s age in this nested list?

A

names_ages[2][1]

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

set_1 = {2, 4, 6, 8}
set_2 = {1, 2, 5, 6, 7, 8}

What operation would I run to get a result set with all of the elements from both sets?

A

set_1.union(set_2)

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

Code Editor:
names_ages = {‘John’: 35, ‘Jim’: 45, ‘Alice’: 25}

and a second dictionary as below:

Code Editor:
updated_names_ages = {‘Ella’: 29, ‘John’: 36}

How would you update the names_ages dictionary with the values in updated_names_ages dictionary?

A

names_ages.update(updated_names_ages)

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

names_ages = {‘John’: 35, ‘Jim’: 45, ‘Alice’: 25}

What would the output be if you were to run this code?

names_ages[‘Tim’]

A

KeyError: ‘Tim’

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

set

A

you canot add list,dictionary to set
you can add tuple,strings and floats to set

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