Hashmaps Hacks
Observing hashmaps with python dictionaries
import pprint #import library
nba_teams = {
"League": "NBA",
"Country": "North America",
"Current Season": '2022-2023',
"Teams": {
1: "Boston Celtics",
2: "Chicago Bulls",
3: "Toronto Raptors",
4: "Los Angeles Lakers",
5: "Golden State Warriors",
}
}
# Printing the dictionary
pprint.pprint(nba_teams)
pprint.pprint(nba_teams.get('Teams'))
pprint.pprint(nba_teams.get('League'))
nba_teams["Best Players"] = (['Lebron James', 'Stephen Curry', 'Jimmy Butler', 'Joel Embiid', 'Ja Morant', 'Giannis Antetokoumpo', 'Lebron James'])
# What can you change to make sure there are no duplicate players?
# Take out a duplicate
# Make it into a set
# Printing the dictionary
pprint.pprint(nba_teams)
print("Original List: ", nba_teams['Best Players'])
res = [*set(nba_teams['Best Players'])]
print("List after removing duplicate elements: ", res)
nba_teams["Teams"].update({6:"New York Knicks"})
# How would add an additional team
# use .update
# filter to teams, add new number key they type team name
# Printing the dictionary
pprint.pprint(nba_teams)
for k,v in nba_teams.items(): # iterate using a for loop for key and value
print(str(k) + ": " + str(v))
# Write your own code to print tracks in readable format
# use pprint library
def search():
search = input("What would you like to know about the NHL? \n For teams type --> Teams")
if nba_teams.get(search.lower()) == None:
print("North America")
else:
pprint.pprint(nba_teams.get(search.lower()))
search()
Hacks
- Answer ALL questions in the code segments
- Create a diagram or comparison illustration (Canva).
- What are the pro and cons of using this data structure?
- Dictionary vs List
- Expand upon the code given to you, possible improvements in comments
-
Build your own album showing features of a python dictionary
-
For Mr. Yeung's class: Justify your favorite Taylor Swift song, answer may effect seed.