Data Types - Dictionary Flashcards

1
Q

Can Dictionaries be sliced, and why?

A

No - Python is simply not programmed to support it

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

person = {“name”: “Alice”, “age”: 30}

Give 2 ways to remove “age” from this dictionary

A

del person[“age”]

person.pop(“age”)

.remove() is a method of List

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

scores = {“Alice”: 90, “Bob”: 75, “Clara”: 88}

Loop through “scores” and print “High score” if score is >= 80

A

for score in scores.values():
if score >= 80:
print(“High score”)

.values() is a method of Dictionaries which produces an “iterator-like view object” which can be looped through.

Output syntax:
dict_values([value1, value2, value3])

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

What is .values()

A

.values() is a method of Dictionaries which produces an “iterator-like view object” which can be looped through.

Output syntax:
dict_values([value1, value2, value3])

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

scores = {“Alice”: 90, “Bob”: 75, “Clara”: 88}

Convert the above to a List, only containing the “scores”(values)

A

score_list = list(scores.values())

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