Data Types - Dictionary Flashcards
Can Dictionaries be sliced, and why?
No - Python is simply not programmed to support it
person = {“name”: “Alice”, “age”: 30}
Give 2 ways to remove “age” from this dictionary
del person[“age”]
person.pop(“age”)
.remove() is a method of List
scores = {“Alice”: 90, “Bob”: 75, “Clara”: 88}
Loop through “scores” and print “High score” if score is >= 80
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])
What is .values()
.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])
scores = {“Alice”: 90, “Bob”: 75, “Clara”: 88}
Convert the above to a List, only containing the “scores”(values)
score_list = list(scores.values())