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)
{'Country': 'North America',
 'Current Season': '2022-2023',
 'League': 'NBA',
 'Teams': {1: 'Boston Celtics',
           2: 'Chicago Bulls',
           3: 'Toronto Raptors',
           4: 'Los Angeles Lakers',
           5: 'Golden State Warriors'}}
pprint.pprint(nba_teams.get('Teams'))
{1: 'Boston Celtics',
 2: 'Chicago Bulls',
 3: 'Toronto Raptors',
 4: 'Los Angeles Lakers',
 5: 'Golden State Warriors'}
pprint.pprint(nba_teams.get('League'))
'NBA'
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)
{'Best Players': ['Lebron James',
                  'Stephen Curry',
                  'Jimmy Butler',
                  'Joel Embiid',
                  'Ja Morant',
                  'Giannis Antetokoumpo',
                  'Lebron James'],
 'Country': 'North America',
 'Current Season': '2022-2023',
 'League': 'NBA',
 'Teams': {1: 'Boston Celtics',
           2: 'Chicago Bulls',
           3: 'Toronto Raptors',
           4: 'Los Angeles Lakers',
           5: 'Golden State Warriors'}}
print("Original List: ", nba_teams['Best Players'])
res = [*set(nba_teams['Best Players'])]
print("List after removing duplicate elements: ", res)
Original List:  ['Lebron James', 'Stephen Curry', 'Jimmy Butler', 'Joel Embiid', 'Ja Morant', 'Giannis Antetokoumpo', 'Lebron James']
List after removing duplicate elements:  ['Joel Embiid', 'Ja Morant', 'Stephen Curry', 'Jimmy Butler', 'Giannis Antetokoumpo', 'Lebron James']
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)
{'Best Players': ['Lebron James',
                  'Stephen Curry',
                  'Jimmy Butler',
                  'Joel Embiid',
                  'Ja Morant',
                  'Giannis Antetokoumpo',
                  'Lebron James'],
 'Country': 'North America',
 'Current Season': '2022-2023',
 'League': 'NBA',
 'Teams': {1: 'Boston Celtics',
           2: 'Chicago Bulls',
           3: 'Toronto Raptors',
           4: 'Los Angeles Lakers',
           5: 'Golden State Warriors',
           6: 'New York Knicks'}}
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
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', 6: 'New York Knicks'}
Best Players: ['Lebron James', 'Stephen Curry', 'Jimmy Butler', 'Joel Embiid', 'Ja Morant', 'Giannis Antetokoumpo', 'Lebron James']
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()
North America

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.